没有静态类型的C#接口

zrg*_*zrg 10 c#

有没有办法沿着这些方向做点什么?

interface Iface
{
  [anytype] Prop1 { get; }
  [anytype] Prop2 { get; }
}

class Class1 : Iface
{
  public string Prop1 { get; }
  public int Prop2 { get; }
}

class Class2 : Iface
{
  public int Prop1 { get; }
  public bool? Prop2 { get; }
}
Run Code Online (Sandbox Code Playgroud)

我不关心属性的类型,我只需要可用的属性.这不必使用接口实现,仅使用它作为示例.

LBu*_*kin 25

使界面通用:

interface Iface<T1,T2>
{
   T1 Prop1  { get; }
   T2 Prop2  { get; }
}
Run Code Online (Sandbox Code Playgroud)

或者,创建类型的属性object:

interface Iface
{
   object Prop1  { get; }
   object Prop2  { get; }
}
Run Code Online (Sandbox Code Playgroud)

如果您使用的是.NET 4.0,您甚至可以创建类型的属性dynamic:

interface Iface    {
   dynamic Prop1  { get; }
   dynamic Prop2  { get; }
}
Run Code Online (Sandbox Code Playgroud)


kbr*_*ton 5

使用object,或通用Iface<TValue1,TValue2>