Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# 4.0 C#:获取从接口实现的类的属性_C# 4.0_Interface_Properties_System.reflection - Fatal编程技术网

C# 4.0 C#:获取从接口实现的类的属性

C# 4.0 C#:获取从接口实现的类的属性,c#-4.0,interface,properties,system.reflection,C# 4.0,Interface,Properties,System.reflection,我一直在尝试在实现接口的类中获取属性。我的设计如下: interface ABC { string Name { get; set; } } public class BCD:ABC { public string Name { get; set; } public string Age{ get; set; } public string Height{ get; set; } public string Weight{ get; set; } } 现在使用反射我已经试过了 m

我一直在尝试在实现接口的类中获取属性。我的设计如下:

interface ABC
{
  string Name { get; set; }
}

public class BCD:ABC
{
 public string Name { get; set; }
 public string Age{ get; set; }
 public string Height{ get; set; }
 public string Weight{ get; set; }
}
现在使用反射我已经试过了

main()
{
  ABC abcObj = new BCD();
  var typeOfObject = typeof(abcObj);
  var objectProperties = typeOfObject.GetProperties(BindingFlags.Public|BindingFlags.Instance);
}
我在objectproperties中得到的是ABC类中的属性。但是,我也需要BCD类的属性


有人能帮上忙吗?

不要使用
typeof
(这适用于类/接口名称),试试看

获取实例的类型

当我跑的时候

ABC abcObj = new BCD();
var objectProperties = abcObj.GetType()
            .GetProperties(BindingFlags.Public | BindingFlags.Instance);

for (int i = 0; i < objectProperties.Length; i++)
{
    Console.WriteLine("{0} ({1})", objectProperties[i].Name, objectProperties[i].PropertyType);
}
ABC abcObj=new BCD();
var objectProperties=abcObj.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance);
for(int i=0;i
我在控制台中获得以下信息:

名称(System.String)

年龄(System.String)

高度(系统字符串)

重量(系统字符串)


不要使用
typeof
(这适用于类/接口名称),请尝试

获取实例的类型

当我跑的时候

ABC abcObj = new BCD();
var objectProperties = abcObj.GetType()
            .GetProperties(BindingFlags.Public | BindingFlags.Instance);

for (int i = 0; i < objectProperties.Length; i++)
{
    Console.WriteLine("{0} ({1})", objectProperties[i].Name, objectProperties[i].PropertyType);
}
ABC abcObj=new BCD();
var objectProperties=abcObj.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance);
for(int i=0;i
我在控制台中获得以下信息:

名称(System.String)

年龄(System.String)

高度(系统字符串)

重量(系统字符串)


你试过把它当作BCD类吗?你试过把它当作BCD类吗?