无论如何,我可以得到类属性的名称
IntProperty
?
public class ClassName
{
public static int IntProperty { get { return 0; } }
}
//something like below but I want to get the string of "IntProperty"
ClassName.IntProperty.GetType().Name
我想做的是基本地将属性名称字符串动态保存到数据库中,然后从数据库中检索并动态调用属性。
看起来像我想要的类似于我认为的鸭子打字。 thanks!
任期:
这是实际的代码。这更像是工作流程。但是每个任务都定义为类的属性(类用于分组任务)。
public class ApplicationTask
{
public static Task<string> SendIncompleteNotification
{
get
{
return new Task<string>
(
a => Console.WriteLine("Sample Task")
, "This is a sample task which does nothing."
);
}
}
}
SO,代码将能够检索类和属性的全名,例如:
namespace.ApplicationTask.SendIncompleteNotification
C#6.0可以通过
nameof(ClassName.IntProperty)
getProperty
方法是多余的,因为您需要知道调用该方法的属性名称。您可以循环浏览您的属性并提取其名称:
Expression<Func<int>> exp = () => ClassName.IntProperty;
然后,您可以编译并执行表达式
并单独找出它在做什么(在这种情况下为检索)。我不太确定这是否适合您想做的事情。如果您 也许,如果您可以在您想如何使用此问题方面给出更多的上下文,那么我们可以提供更多帮助。您只显示了一个表达式 - 如果您可以用它在哪里使用它来显示它,那就太好了。
Edit:您已经扩展了属性,但没有被称为属性。您是否直接调用它,而不仅仅是使用
IntProperty
获取属性列表并存储数据库中的属性名称列表?
,如果您可以显示该属性的代码,以及您希望它与数据库交互的代码,我们可能能够取得更大的进步。
GetProperty
这是您需要的吗?您可以简单地使用
Type.GetProperties
它会给您“ intproperty”
尝试此示例代码
Type objectType = this.GetType();
PropertyInfo property = objectType.GetProperty("intProperty");
System.Console.Write(property.Name);
和另一个.cs文件。
nameof(ClassName.IntProperty)
string []Name = new PropertiesOfClassCostmer().fnNameOfClassCostmer;
ref:
http://zobayer.blogspot.com/2010/05/c-fun-get-variables-name.html