Csharp/C#教程:将属性传递给C#中的方法分享


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

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

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

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

 List propertyList = new List(); propertyList.Add(Product.Id); PropertyList.Add(Product.Title); TargetType target = new TargetType(); target.Properties = propertyList; public class TargetType { public List Properties { get; set;} GetAttributes() { foreach(Property item in Properties){ Console.WriteLine(item.Name) //Get Attributes } } } 

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

有趣的是,我只是在回答类似的问题,或者至少我认为是这样。

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

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

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

C#深度/嵌套/递归合并dynamic / expando对象

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

 var props = object.GetType().GetProperties().ToDictionary(prop => prop.Name); 

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

你可以在这里使用.NET中的reflection:

 List propertyList = new List(); 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 Properties { get; set;} List GetAttributes() { List attributes = new List(); foreach(PropertyInfo item in Properties) { Console.WriteLine(item.Name); attributes.AddRange(item.GetCustomAttributes(true)); } return attributes; } } 

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

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

您可以使用表达式树构建属性列表,例如,您可以创建如下内容:

 var propertiesListBuilder = new PropertiesListBuilder(); propertiesListBuilder .AddProperty(_ => _.Id) .AddProperty(_ => _.Title); var target = new TargetType(); target.Properties = propertiesListBuilder.Properties; var propertiesListBuilder = new PropertiesListBuilder(); propertiesListBuilder .AddProperty(_ => _.Id) .AddProperty(_ => _.Title); var target = new TargetType(); target.Properties = propertiesListBuilder.Properties; 

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

下面是这个东西的示例实现。

 static class PropertyInfoProvider { public static PropertyInfo GetPropertyInfo(Expression> expression) { var memberExpression = (MemberExpression)expression.Body; return (PropertyInfo)memberExpression.Member; } } class PropertiesListBuilder { public IEnumerable Properties { get { return this.properties; } } public PropertiesListBuilder AddProperty( Expression> expression) { var info = PropertyInfoProvider.GetPropertyInfo(expression); this.properties.Add(info); return this; } private List properties = new List(); } static class PropertyInfoProvider { public static PropertyInfo GetPropertyInfo(Expression> expression) { var memberExpression = (MemberExpression)expression.Body; return (PropertyInfo)memberExpression.Member; } } class PropertiesListBuilder { public IEnumerable Properties { get { return this.properties; } } public PropertiesListBuilder AddProperty( Expression> expression) { var info = PropertyInfoProvider.GetPropertyInfo(expression); this.properties.Add(info); return this; } private List properties = new List(); } 

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

另请参见MSDN 。

上述就是C#学习教程:将属性传递给C#中的方法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/997956.html

(1)
上一篇 2021年12月26日
下一篇 2021年12月26日

精彩推荐