相关疑难解决方法(0)

如何获取特定属性的PropertyInfo?

我想获取特定属性的PropertyInfo.我可以用:

foreach(PropertyInfo p in typeof(MyObject).GetProperties())
{
    if ( p.Name == "MyProperty") { return p }
}
Run Code Online (Sandbox Code Playgroud)

但必须有办法做类似的事情

typeof(MyProperty) as PropertyInfo
Run Code Online (Sandbox Code Playgroud)

在那儿?还是我坚持做一个类型不安全的字符串比较?

干杯.

c# reflection

77
推荐指数
3
解决办法
6万
查看次数

我什么时候应该使用C#Generic Func <T,Tresult>?

我一直在看C#的通用函数委托(Func)功能.

例:

// Instantiate delegate to reference UppercaseString method
Func<string, string> convertMethod = UppercaseString;
string name = "Dakota";

// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMethod(name));
Run Code Online (Sandbox Code Playgroud)

我正在努力想到一个真实的生活场景,它们可能在我自己的应用程序中很有用.所以我想我会把问题提到那里.\

我非常感谢你的想法.

c# generics func

2
推荐指数
1
解决办法
1886
查看次数

标签 统计

c# ×2

func ×1

generics ×1

reflection ×1