C# 扩展类的初始化?
我从某处获得AAA类的对象,我想在该对象中添加更多信息。因此,我创建了一个新的类BBB,它是从AAA派生的。类BBB有额外的字段字典。我在派生类构造函数中填充这个字典,派生类构造函数采用AAA类对象和要用作字典键的项数组,这个字典的值是AAA类对象字段的元素。我尝试在示例代码中创建类似的场景:C# 扩展类的初始化?,c#,inheritance,C#,Inheritance,我从某处获得AAA类的对象,我想在该对象中添加更多信息。因此,我创建了一个新的类BBB,它是从AAA派生的。类BBB有额外的字段字典。我在派生类构造函数中填充这个字典,派生类构造函数采用AAA类对象和要用作字典键的项数组,这个字典的值是AAA类对象字段的元素。我尝试在示例代码中创建类似的场景: void Main(){ A obj = new A () ; obj.prop1 = new int [] {5 ,10, 15} ; obj.prop2 =
void Main(){
A obj = new A () ;
obj.prop1 = new int [] {5 ,10, 15} ;
obj.prop2 = "Hello" ;
obj.prop3 = "World" ;
// obj.Dump () ;
B obj2 = new B (new int [] {1,2,3}, obj) ;
// obj2.Dump () ;
}
// Define other methods and classes here
public class A {
public int [] prop1 ;
public string prop2 ;
public string prop3 ;
}
public class B : A {
public Dictionary <int, int> prop4 ;
public B (int [] keys, A a) {
prop4 = new Dictionary <int, int> () ;
if (keys.Length == a.prop1.Length ) {
for (int i = 0 ; i < keys.Length ; i++ ) {
prop4.Add (keys[i], a.prop1[i]) ;
}
// is there a way to obsolete below lines of code???
this.prop1 = a.prop1 ;
this.prop2 = a.prop2 ;
this.prop3 = a.prop3 ;
}
else {
throw new Exception ("something wrong") ;
}
}
}
void Main(){
A obj=新的A();
obj.prop1=新的int[]{5,10,15};
obj.prop2=“你好”;
obj.prop3=“世界”;
//obj.Dump();
B obj2=新的B(新的int[]{1,2,3},obj);
//obj2.Dump();
}
//在此处定义其他方法和类
公共A类{
公共int[]prop1;
公共字符串prop2;
公共字符串prop3;
}
B级:A级{
公共词典prop4;
公共B(int[]键,A){
prop4=新字典();
if(keys.Length==a.prop1.Length){
for(int i=0;i
在派生类构造函数中,我正在手动填充属性,我不想这样做。还有别的办法吗。在我的实际类中,我有20多个属性。您正在构造的
B
实例和A
实例(您正在传递给B
的构造函数)不相关-它们持有不同的数据,它们位于不同的内存地址上。从传递的A
的值更新B
实例的属性/字段的唯一方法是复制。hmmmmm。。。这有点不清楚,让人困惑,但看看这个:
也许这与你需要什么无关,但至少它可能会给你一个想法
这就是如何从类型的所有属性创建字典的方法
public static Dictionary<string, object> DictionaryFromType(object atype)
{
if (atype == null) return new Dictionary<string, object>();
Type t = atype.GetType();
PropertyInfo[] props = t.GetProperties();
Dictionary<string, object> dict = new Dictionary<string, object>();
foreach (PropertyInfo prp in props)
{
object value = prp.GetValue(atype, new object[]{});
dict.Add(prp.Name, value);
}
return dict;
}
publicstaticdictionary字典fromtype(objectatype)
{
if(atype==null)返回新字典();
类型t=atype.GetType();
PropertyInfo[]props=t.GetProperties();
Dictionary dict=新字典();
foreach(道具中prp的属性信息)
{
objectvalue=prp.GetValue(atype,新对象[]{});
dict.Add(prp.Name,value);
}
返回命令;
}
也许能帮你。如果不让我知道,请删除此问题。您在这里实现的与
复制构造函数非常相似。恐怕只有手工做了
我的建议是在A类定义中使用属性
,并覆盖B类实现的属性:
// Define other methods and classes here
public class A
{
public virtual int[] prop1 { get; set; }
public virtual string prop2 { get; set; }
public virtual string prop3 { get; set; }
}
public class B : A
{
public Dictionary<int, int> prop4;
public override int[] prop1
{
get
{
return m_WrappedAInstance.prop1;
}
set
{
m_WrappedAInstance.prop1 = value;
}
}
public override string prop2
{
get
{
return m_WrappedAInstance.prop2;
}
set
{
m_WrappedAInstance.prop2 = value;
}
}
public override string prop3
{
get
{
return m_WrappedAInstance.prop3;
}
set
{
m_WrappedAInstance.prop3 = value;
}
}
A m_WrappedAInstance = null;
public B(int[] keys, A a)
{
m_WrappedAInstance = a;
prop4 = new Dictionary<int, int>();
if (keys.Length == a.prop1.Length)
{
for (int i = 0; i < keys.Length; i++)
{
prop4.Add(keys[i], a.prop1[i]);
}
}
else
{
throw new Exception("something wrong");
}
}
}
//在此处定义其他方法和类
公共A类
{
公共虚拟int[]prop1{get;set;}
公共虚拟字符串prop2{get;set;}
公共虚拟字符串prop3{get;set;}
}
B级:A级
{
公共词典prop4;
公共覆盖int[]prop1
{
得到
{
返回m_WrappedAInstance.prop1;
}
设置
{
m_WrappedAInstance.prop1=值;
}
}
公共重写字符串prop2
{
得到
{
返回m_WrappedAInstance.prop2;
}
设置
{
m_WrappedAInstance.prop2=值;
}
}
公共重写字符串prop3
{
得到
{
返回m_WrappedAInstance.prop3;
}
设置
{
m_WrappedAInstance.prop3=值;
}
}
m_WrappedAInstance=null;
公共B(int[]键,A)
{
m_WrappedAInstance=a;
prop4=新字典();
if(keys.Length==a.prop1.Length)
{
for(int i=0;i
在功能上,这将起到类似的作用。唯一的缺点是包装的类A实例不能再独立于类B实例进行更改。这是一项要求吗?无法满足您的要求,但我建议为类a
创建一个副本构造函数,并将其与类B
构造函数一起使用:
// Define other methods and classes here
public class A
{
public int[] prop1;
public string prop2;
public string prop3;
public A()
{
}
public A(A orig)
{
prop1 = orig.prop1;
prop2 = orig.prop2;
prop3 = orig.prop3;
}
}
public class B : A
{
public Dictionary<int, int> prop4;
public B(int[] keys, A a) : base( a )
{
prop4 = new Dictionary<int, int>();
if (keys.Length == prop1.Length)
{
for (int i = 0; i < keys.Length; i++)
{
prop4.Add(keys[i], prop1[i]);
}
}
else
{
throw new Exception("something wrong");
}
}
}
//在此处定义其他方法和类
公共A类
{
公共int[]prop1;
公共字符串prop2;
公共字符串prop3;
公共A()
{
}
公共A(原A)
{
prop1=原始prop1;
prop2=原始prop2;
prop3=原始prop3;
}
}
B级:A级
{
公共词典prop4;
公共B(int[]键,A):基(A)
{
prop4=新字典();
if(keys.Length==prop1.Length)
{
for(int i=0;i
定义一个数组。然后将数组中的项强制转换为单个属性
A班
public object[] properties;
public int[] prop1 {
get { return (int[]) properties[0]; }
set { properties[0] = value; } }
public string prop2 {
get { return (string)properties[1]; }
set { properties[1] = value ; } }
B的构造函数
public B (int[] keys, A obj)
{
properties = A.properties;
}
看起来你更想要一个索引器?看起来您希望通过键访问整数数组中的值-您能解释一下这个用例吗?一旦你这样做了一次,你就这样做了-你打算对其他对象这样做一百万次吗?名称prop1
、prop2
、prop3
、和prop4
都非常糟糕。它们不代表相同的类型,也不以相同的方式使用。因此,如果你给他们起的名字更清晰,那将是非常有帮助的