Pass Property to the Method in C#

此生再无相见时 提交于 2019-12-08 01:06:31

问题


I need to pass selection of properties of some types(one type each time), assume this is my type:

public class Product {

    [PrimaryKey]
    public long Id { get; set; }
    [DisplayName("Name")]
    public string Title { get; set; }
    [Foreignkey(Schema = "Products", Table = "MajorCategory", Column = "Id")]
    [DisplayName("MCat")]
    public string MajorCategory { get; set; }
    [Foreignkey(Schema = "Products", Table = "Category", Column = "Id")]
    [DisplayName("Cat")]
    public string Category { get; set; }
    public long CategoryId { get; set; }
    [BoolAsRadio()]
    public bool IsScanAllowed { get; set; }
}

So I need a way to pass the list of properties of this type to other Type(Target Type), and use property name, and attributes, and I don't need values, something like the following Pseudo-code:

List<Property> propertyList = new List<Property>();
propertyList.Add(Product.Id);
PropertyList.Add(Product.Title);
TargetType target = new TargetType();
target.Properties = propertyList;


public class TargetType  {

   public List<Property> Properties { get; set;}

   GetAttributes() {

      foreach(Property item in Properties){

         Console.WriteLine(item.Name)

         //Get Attributes
      }
   }


}

Is there any way to pass just like Product.Id and use name and attributes of that? I don't sure but maybe PropertyInfo can help, I think just can pass List of Object but in that case I can't use attributes and names, what is your suggestion to handle this? or something like this? if I am wrong at all so how can I implement it?


回答1:


Funny, I was just answering a similar question, or at least I think it is.

It looks like you're trying to concatenate the properties of two types into one? You need an ExpandoObject:

http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject%28v=vs.100%29.aspx

For an implementation of a nested merge, see this:

C# deep/nested/recursive merge of dynamic/expando objects

Basically, you want a keyed list of properties, to start from. The following code will do that for any .NET object:

var props = object.GetType().GetProperties().ToDictionary<PropertyInfo, string>(prop => prop.Name);

And after that it depends on what precisely it is you want to achieve - a true copy of the object, merge with another, or just maintaining the list.




回答2:


You can make use of reflection in .NET here:

List<PropertyInfo> propertyList = new List<PropertyInfo>();
Type productType = typeof (Product);

propertyList.Add(productType.GetProperty("Id"));
propertyList.Add(productType.GetProperty("Title"));
TargetType target = new TargetType();
target.Properties = propertyList;

public class TargetType  {

   public List<PropertyInfo> Properties { get; set;}

   List<object> GetAttributes()
   {
       List<object> attributes = new List<object>();

       foreach(PropertyInfo item in Properties)
       {
           Console.WriteLine(item.Name);
           attributes.AddRange(item.GetCustomAttributes(true));
       }

       return attributes;
   }
}



回答3:


You can use a list of PropertyInfo, List<PropertyInfo> as the type of your TargetType .Properties. To get the properties you can try it like this using Reflection.

targetType.Properties = product.GetType().GetProperties().ToList();



回答4:


You can build list of properties using expression trees, e.g. you can make something like this:


var propertiesListBuilder = new PropertiesListBuilder<Product>();
propertiesListBuilder
    .AddProperty(_ => _.Id)
    .AddProperty(_ => _.Title);

var target = new TargetType();
target.Properties = propertiesListBuilder.Properties;

The only concern here is performance, i.e. it might be not good idea to recreate such property lists over and over again, most probably they should be cached. At the same time you'll get intellisense, compiler checks and refactoring support for your property lists.

Below is a sample implementation of this stuff.


static class PropertyInfoProvider<T>
{
    public static PropertyInfo GetPropertyInfo<TProperty>(Expression<Func<T, TProperty>> expression)
    {
        var memberExpression = (MemberExpression)expression.Body;

        return (PropertyInfo)memberExpression.Member;
    }
}

class PropertiesListBuilder<T>
{
    public IEnumerable<PropertyInfo> Properties
    {
        get
        {
            return this.properties;
        }
    }

    public PropertiesListBuilder<T> AddProperty<TProperty>(
        Expression<Func<T, TProperty>> expression)
    {
        var info = PropertyInfoProvider<T>.GetPropertyInfo(expression);
        this.properties.Add(info);

        return this;
    }

    private List<PropertyInfo> properties = new List<PropertyInfo>();
}




回答5:


typeof(Product).GetProperties() would give you all (public) properties as PropertyInfo[].

See also MSDN.



来源:https://stackoverflow.com/questions/10205026/pass-property-to-the-method-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!