Ant*_*lin 2 .net c# system.reflection .net-core
我需要能够访问.NET Core中类的当前属性的名称.
public class MyClass
{
private string _myProperty;
public string MyProperty
{
get => _myProperty;
set
{
// GOAL: Check my property name and then do something with that information.
// The following used to works in the full .NET Framework but not .NET Core.
// var propName = System.Reflection.MethodBase.GetCurrentMethod().Name;
_myProperty = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在完整的.NET Framework中,我们可以使用如下的反射.
System.Reflection.MethodBase.GetCurrentMethod().Name;
Run Code Online (Sandbox Code Playgroud)
(我们记得,一个房产实际上只是一种方法.)
但是,它似乎没有在.NET Core 1.1中提供此功能 - 尽管它(可能/将)在.NET Standard 2.0中可用.
在这个GitHub问题(https://github.com/dotnet/corefx/issues/12496)上,有一个GetMethodInfoOf用于访问属性名称的引用.但是,我无法找到此方法的任何示例,并且指向可能示例的GitHub链接似乎已断开.
可以使用哪种其他策略或技术来检索当前属性的名称?
非常感谢!
也许我完全误解了你的问题,但仅仅使用nameof运算符呢?
var propName = nameof(MyProperty);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
830 次 |
| 最近记录: |