Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
覆盖值c#_C#_.net_Performance_Reflection - Fatal编程技术网

覆盖值c#

覆盖值c#,c#,.net,performance,reflection,C#,.net,Performance,Reflection,我的情况是,我有3个类,如下所示: public class A { public int Id { get; set; } public int BranchId { get; set; } public int LocationId { get; set; } public int DepID { get; set; } public int Age { get; set; } public string Name { get; set; }

我的情况是,我有3个
,如下所示:

public class A
{
    public int Id { get; set; }
    public int BranchId { get; set; }
    public int LocationId { get; set; }
    public int DepID { get; set; }
    public int Age { get; set; }
    public string Name { get; set; }
}

public class B
{
    public int LocationId { get; set; }
    public int DepID { get; set; }
    public int Age { get; set; }
    public string Name { get; set; }
}  

public class C
{
    public int Age { get; set; }
    public string Name { get; set; }
}
从上面的
中,我们看到b/w 3
中几乎没有公共属性

在这种情况下,我必须通过组合3个
中的值来创建最终输出,并在较低的
中存在和值时覆盖它们

例如:

考虑所有3个
类别
A
B
C
的物业年限,但如果
C
中的值不为
null
,则合并
类别
C
中的值会优先,所有物业都是这样

我已经通过使用
实现了这一点,通过使用
.GetType()
.GetProperties()
方法从所有3个
类的实例中读取值,这些方法是
系统.Reflection
库的一部分

使用当前基于反射的
方法,我将能够处理任何
类中添加的属性

但是我读到,
反射
的性能很差


那么有更好的方法吗?

您需要了解继承。您的数据结构应如下所示:

public class C
{
    public int Age { get; set; }
    public string Name { get; set; }
}

public class B : C
{
    public int LocationId { get; set; }
    public int DepID { get; set; }
}  

public class A : B
{
    public int Id { get; set; }
    public int BranchId { get; set; }
}

您只需创建正确类型的对象,每个对象都具有所需的属性,但不会获得重复的属性。

您可以使用继承将公共属性下推到基类,然后从它们派生来创建其他属性,例如:

public class A : B
{
    public int Id { get; set; }
    public int BranchId { get; set; }
}

public class B : C
{
    public int LocationId { get; set; }
    public int DepID { get; set; }
}  

public class C
{
    public int Age { get; set; }
    public string Name { get; set; }
}

接口可用于标识属性组。这种方法不会强制您更改已建立的类层次结构

 interface IData1
 {
     int Id { get;set; }
     int BranchId { get;set; }
 }

 interface IData2 
 {
     int LocationId { get;set; }
     int DepID { get;set; }
 }

 interface IData3
 {
      int Age { get;set; }
      string Name { get;set; }
 }


 public class A : IData1, IData2, IData3
 {
      public int Id { get;set; }
      public int BranchId { get;set; }
      public int LocationId { get;set; }
      public int DepID { get;set; }
      public int Age { get;set; }
      public string Name { get;set; }
 }

 public class B : IData2, IData3
 {
     public int LocationId { get;set; }
     public int DepID { get;set; }
     public int Age { get;set; }
     public string Name { get;set; }
 }

 public class C : IData3
 {
     public int Age { get;set; }
     public string Name { get;set; }
  }

 void WriteData1(IData1 data){}
 void WriteData2(IData2 data){}
 void WriteData3(IData3 data){}

 void WriteProperties(A a, B b, C c)
 {
       WriteData1(a);

       WriteData2(a);
       WriteData2(b);

       WriteData3(a);
       WriteData3(b);
       WriteData3(c); 
  }

你需要看看继承。将公共属性放在基类中,并由此派生出另一个类。这个问题是关于软件设计模式的,不是吗?这听起来像是家庭作业。