如何获得一个类属性的名称? [英] How to get name of a class property?

查看:217
本文介绍了如何获得一个类属性的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有反正我能得到类属性的名称 IntProperty

Is there anyway I can get the name of class property 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

基本上我想要做的就是属性名字符串动态地保存到数据库中,后来就从数据库中检索和调用动态属性。

Basically what I want to do is to dynamically save property name string into the database, and later on retrieve it from the database and invoke the property dynamically.

好像我所寻找的是类似鸭打字,我认为。

Seems like what I am looking for is similar to duck typing I think.

谢谢!

更新:

这是实际的代码。这更像是一个工作流之类的话。但是,每个任务被定义为一类财产(类用于组任务)。

This is the actual code. This is more like a workflow kind of thing. But each task is defined as property of a class (class is used to group tasks).

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."
                );
        }
    }
}



因此,代码会能够获取的类和属性类似的全名: namespace.ApplicationTask.SendIncompleteNotification 并保存到数据库中这一点。后来,该代码将读取字符串,并动态地创建任务,并把它传递到另一个执行。

So, the code will be able to retrieve the full name of the class and property something like: namespace.ApplicationTask.SendIncompleteNotification and save this into the database. Later on, the code will read the string and dynamically create the task and pass it into another to execute.

推荐答案

结果 ClassName.IntProperty 仅仅是一个整数值。只要它的执行并返回结果,有有来自 IntProperty 没有它的踪影。

The result of ClassName.IntProperty is just an integer value. As soon as it's executed and the result is returned, there's no trace of it having come from IntProperty.

如果您正在使用.NET 3.5,你可以改用,通常是通过一个lambda表达式创建的表达式树:

If you're using .NET 3.5 you can use an expression tree instead, usually created via a lambda expression:

Expression<Func<int>> exp = () => ClassName.IntProperty;



然后,您可以编译和执行表达式的的单独找出它的做(检索 IntProperty 在这种情况下)。我真的不知道这是否是适合你想要虽然做什么。

You can then compile and execute the expression and separately find out what it's doing (retrieving IntProperty in this case). I'm not really sure whether this is suitable for what you want to do though.

如果您的的工作,如何救楼市命名数据库,然后的getProperty 是去检索前面的路。

If you do work out how to save the property name in the database, then GetProperty is the way to go on the retrieval front.

或许,如果你能给在你想如何使用这个术语的问题更多的上下文,我们可以帮助更多。你已经证明只是一种表达 - 如果你能在那里你会使用它,那将是巨大的方面表现出来。

Perhaps if you could give more context in the question in terms of how you want to use this, we could help more. You've shown just an expression - if you could show it in terms of where you'd be using it, that would be great.

编辑:你已经扩大财产,但它并不怎么被调用。你的需求的直接调用它,而不是仅仅利用获取的属性列表 Type.GetProperties ,并在数据库中存储属性名称的列表?

You've expanded the property, but not how it's being called. Do you need to call it directly, rather than just fetching the list of properties using Type.GetProperties and storing the list of property names in the database?

同样,如果你能证明它的代码调用的财产,你想怎么它与数据库进行交互,我们可能能够取得更大的进展。

Again, if you could show the code which calls the property, and how you want it to interact with the database, we may be able to make more progress.

这篇关于如何获得一个类属性的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆