我使用一个包含对象类型参数的函数。我想知道这个未知类型对象的属性的名称。我该怎么做?
KR,
达克马兹
发布于 2011-03-28 14:46:32
使用GetProperties
var properties = obj.GetType().GetProperties();发布于 2011-03-28 15:00:46
不要使用类型为object的参数,而是使用仿制药。
然后,您可以使用约束这个泛型来实现接口或从基类继承。
然后,您将能够访问在受限接口/基类型中定义的属性和函数。您还可以定义自己的接口并对其进行约束。
示例代码:
public void MyFunc<T>(T myParam)
where T : IEnumerable // or some other interface or base class.
{
foreach (var child in myParam) // uses the interface IEnumerable that the generic was constrained to
{
// do something
}
}发布于 2011-03-28 14:46:56
http://www.csharp-examples.net/reflection-property-names/
我使用一个包含对象的函数
我闻到了糟糕的代码设计。
https://stackoverflow.com/questions/5460689
复制相似问题