获取属性名称并将其直接传递通过键入 [英] Get property name and type by pass it directly

查看:139
本文介绍了获取属性名称并将其直接传递通过键入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问类似的问题的这里这里

这是一个示例类型:

    public class Product {

    public string Name { get; set; }
    public string Title { get; set; }
    public string Category { get; set; }
    public bool IsAllowed { get; set; }

}

和我有需要的属性生成一个泛型类一些HTML代码:

And I have a Generic class that needs properties to generate some HTML codes:

public class Generator<T> {

    public T MainType { get; set; }
    public List<string> SelectedProperties { get; set; }

    public string Generate() {

        Dictionary<string, PropertyInfo> props;
        props = typeof(T)
                .GetProperties()
                .ToDictionary<PropertyInfo, string>(prop => prop.Name);

        Type propType = null;
        string propName = "";
        foreach(string item in SelectedProperties) {
            if(props.Keys.Contains(item)) {
                propType = props[item].PropertyType;
                propName = item;

                // Generate Html by propName & propType
            }
        }

和我使用这个类型如下:

And I use this types as following:

Product pr = new Product();
Generator<Product> GT = new Generator<Product>();
GT.MainType = pr;
GT.SelectedProperties = new List<string> { "Title", "IsAllowed" };

GT.Generate();



所以我认为这个过程应该是比较容易的是,但我不知道如何实现它,我想通过属性发电机更简单,有点像如下的伪代码:

So I think this process should be more easy as is but I don't know how implement it, I think to pass properties to the generator more simple, something like the followings Pseudo-code:

GT.SelectedProperties.Add(pr.Title);
GT.SelectedProperties.Add(pr.IsAllowed);



我不知道这是可能的或没有,我只需要两件事1,属性名,如: IsAllowed 2 - 属性类型,如:布尔。也许并不需要通过 MainType 我用它来获得属性类型,所以如果我能处理像上面不需要它了。

I don't know is this possible or not, I need just two things 1-PropertyName like: IsAllowed 2- property type like : bool. Maybe don't need to pass MainType I use it to get property types so if I can handle like the above don't need it any more.

什么是您的建议,以实现这样的事情?

What is your suggestion to implement something like this?

有没有更好的方式来做到这一点?

Is there any better way to do this?

更新

由于 ArsenMkrt 说,我发现可以使用 MemberExpression ,但我不能让物业类型,我看到调试属性类型看到的画面:

As ArsenMkrt said I found can use MemberExpression but I can't get Property Type, I see the property type in debugging see the picture:

所以,我怎么能得到物业类型?

So how can I get property type?

我发现它这里

推荐答案

您可以使用的表达式树,比你的代码看起来像这样

You can use expression tree, than your code will look like this

GT.SelectedProperties.Add(p=>p.Title);
GT.SelectedProperties.Add(p=>p.IsAllowed);

您将需要创建SelectedProperties从列表中派生自定义集合类,并创建添加这个方法

You will need to create custom collection class derived from List for SelectedProperties and create add method like this

   //where T is the type of your class
   public string Add<TProp>(Expression<Func<T, TProp>> expression)
   {
        var body = expression.Body as MemberExpression;
        if (body == null) 
            throw new ArgumentException("'expression' should be a member expression");
        //Call List Add method with property name
        Add(body.Member.Name);
   }



希望这有助于

Hope this helps

这篇关于获取属性名称并将其直接传递通过键入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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