繁体   English   中英

没有公共基类的泛型访问属性

[英]Access Property of Generic without common base class

我有一个库的两个独立类,它们没有相同的基类,我也不能更改这些类的实现。

想象一下这些类是这样的:

public class A {
     public int X { get; } = 1;
}

public class B {
     public int X { get; } = 2;
}

现在我想创建一个泛型类,它要么依赖于A要么依赖于B并在那里访问X的值。

所以我做了:

public class GenericClass<T> 
    /*where T : ?*/
{
    void Foo(T t) {
        int x = t.X; // is this possible and how? 
    }
}

如果我自己实现AB ,我会定义一个实现属性X的接口,但我不能这样做。 在不改变A类和BA情况下,有没有其他方法可以说通用T具有属性X
另一个想法是创建AB子类,然后实现上述接口,但我想避免这种情况。

您可以重载Foo以获取AB

void Foo(A t)
{
  int x = t.X;
}

void Foo(B t)
{
  int x = t.X;
}

如果您想为每个可能具有X属性的类执行此操作,那么您将需要一个基于反射的解决方案。 例如:

void Foo(object obj)
{
  var property = obj.GetType().GetProperty("X");
  if(property == null) throw new Exception();

  int x = (int)property.GetValue(obj);
}

注意:我已经最小化了这里的错误处理。 您需要处理属性可能没有 getter(罕见)或不返回int

如果类的数量是可管理的,那么您可以使用该属性创建一个接口,派生一个新类并实现该接口,这不需要更改。 例如:

interface IMyStuff
{
  int X{get;}
}

class MyA : A, IMyStuff
{
}

class MyB : B, IMyStuff
{
}

不是你可以让 Foo 接受接口:

void Foo(IMyStuff stuff)
{
  int x = stuff.X
}

另一种选择是使用dynamic

dynamic d;
d = t;         // here t can be an instance of A, or or B, or of anything that has X
int x = d.X;

dynamic本质上实现了“鸭子类型”:如果dynamic对象g具有属性X ,则gX将检索它。 这是手动实现反射的替代方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM