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类吗?