将属性传递给C#中的方法

时间:2012-04-18 07:42:05

标签: c# c#-4.0 properties propertyinfo

我需要传递一些类型的属性选择(每次一种类型),假设这是我的类型:

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

所以我需要一种方法将此类型的属性列表传递给其他类型(目标类型),并使用属性名称和属性,我不需要值,如下面的伪代码:< / p>

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
      }
   }


}

有没有办法像Product.Id那样传递并使用它的名称和属性?我不确定,但也许PropertyInfo可以提供帮助,我想可以通过List of Object但是在这种情况下我不能使用属性和名称,你有什么建议来处理这个问题?或类似的东西?如果我错了,我该如何实施呢?

5 个答案:

答案 0 :(得分:1)

好笑,我刚回答了一个类似的问题,或者至少我认为是。

看起来你正在尝试将两种类型的属性连接成一个?你需要一个ExpandoObject:

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

有关嵌套合并的实现,请参阅:

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

基本上,您需要一个键控的属性列表,从头开始。以下代码将对任何.NET对象执行此操作:

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

之后,它取决于你想要实现的精确内容 - 对象的真实副本,与另一个对象合并,或者只是维护列表。

答案 1 :(得分:1)

您可以在此处使用.NET中的反射:

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

答案 2 :(得分:0)

您可以使用PropertyInfoList<PropertyInfo>列表作为TargetType .Properties的类型。要获取属性,您可以使用Reflection尝试这样做。

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

答案 3 :(得分:0)

您可以使用表达式树构建属性列表,例如你可以做这样的事情:


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

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

这里唯一关注的是性能,即一遍又一遍地重新创建这样的属性列表可能不是一个好主意,很可能它们应该被缓存。同时,您将获得智能感知,编译器检查和重构对您的属性列表的支持。

以下是这个内容的示例实现。


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

答案 4 :(得分:-1)

typeof(Product).GetProperties()会将所有(公共)属性视为PropertyInfo[]

另见MSDN

相关问题