我想获取特定属性的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#的通用函数委托(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)
我正在努力想到一个真实的生活场景,它们可能在我自己的应用程序中很有用.所以我想我会把问题提到那里.\
我非常感谢你的想法.