简体   繁体   English

通过直接传递获取属性名称和类型

[英]Get property name and type by pass it directly

I asked similar questions here And here 在这里这里问了类似的问题

This is a sample Type: 这是一个示例类型:

    public class Product {

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

}

And I have a Generic class that needs properties to generate some HTML codes: 我有一个Generic类,需要属性来生成一些HTML代码:

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);

I don't know is this possible or not, I need just two things 1-PropertyName like: IsAllowed 2- property type like : bool . 我不知道这是否可能,我只需要两件事1-PropertyName就像: IsAllowed 2-属性类似: 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. 也许不需要传递MainType我用它来获取属性类型所以如果我能像上面那样处理它不再需要它。

What is your suggestion to implement something like this? 你有什么建议来实现这样的东西?

Is there any better way to do this? 有没有更好的方法来做到这一点?

Update 更新

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: 正如ArsenMkrt说我发现可以使用MemberExpression但我无法获取Property Type,我在调试中看到属性类型看到图片:

在此输入图像描述

So how can I get property type? 那么我怎样才能获得房产类型?

I found it here . 我在这里找到

You can use expression tree , than your code will look like this 您可以使用表达式树 ,而您的代码将如下所示

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

You will need to create custom collection class derived from List for SelectedProperties and create add method like this 您需要创建从List for SelectedProperties派生的自定义集合类,并创建这样的add方法

   //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 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM