C# 通过泛型类';s集合属性
尽管有很多问题被贴出来,但似乎没有一个能在我的问题上帮助我 我已经开始了一次新的C# 通过泛型类';s集合属性,c#,generics,reflection,C#,Generics,Reflection,尽管有很多问题被贴出来,但似乎没有一个能在我的问题上帮助我 我已经开始了一次新的泛型/反射冒险,我只是想了解一下语法和概念 我有一个Generic类,有X个属性,其中一个是集合,一切正常,但我无法按属性名称从集合props中提取值 foreach (var property in typeof(T).GetProperties()) { if (property.Name == "Props") { foreach (var item in (IEnumerabl
泛型
/反射
冒险,我只是想了解一下语法和概念
我有一个Generic
类,有X个属性,其中一个是集合,一切正常,但我无法按属性名称从集合props
中提取值
foreach (var property in typeof(T).GetProperties())
{
if (property.Name == "Props")
{
foreach (var item in (IEnumerable)property.GetValue(type, null))
{
var propertyName = "";
var newValue = "";
var oldValue = "";
sbDescription.AppendLine(strDescriptionVals
.Replace("{0}", (item.ToString() == "PropertyName") ? item.ToString() : "" + ", "));
sbAllNotes.AppendLine(strAllNotes
.Replace("{0}", (item.ToString() == "PropertyName") ? item.ToString() : "")
.Replace("{1}", (item.ToString() == "NewValue") ? item.ToString() : "")
.Replace("{2}", (item.ToString() == "OldValue") ? item.ToString() : ""));
}
}
}
正如您所看到的,我已经确定了属性道具,现在我想通过它循环并按属性名称提取值
foreach (var property in typeof(T).GetProperties())
{
if (property.Name == "Props")
{
foreach (var item in (IEnumerable)property.GetValue(type, null))
{
var propertyName = "";
var newValue = "";
var oldValue = "";
sbDescription.AppendLine(strDescriptionVals
.Replace("{0}", (item.ToString() == "PropertyName") ? item.ToString() : "" + ", "));
sbAllNotes.AppendLine(strAllNotes
.Replace("{0}", (item.ToString() == "PropertyName") ? item.ToString() : "")
.Replace("{1}", (item.ToString() == "NewValue") ? item.ToString() : "")
.Replace("{2}", (item.ToString() == "OldValue") ? item.ToString() : ""));
}
}
}
item.ToString()
只打印类属性的名称空间,而不是值
希望你们这些善良的人能给我指出正确的方向?在玩了代码并进一步理解了逻辑之后,我想到了一个简单的方法,就是在
道具上“获取类型和属性”:
//Get the collection property
foreach (var property in typeof(T).GetProperties())
{
if (property.Name == "Props")
{
foreach (var item in (IEnumerable)property.GetValue(type, null))
{
//Because props is a collection, Getting the type and property on each pass was essential
var propertyName = item.GetType().GetProperty("PropertyName").GetValue(item, null);
var newValue = item.GetType().GetProperty("NewValue").GetValue(item, null);
var oldValue = item.GetType().GetProperty("OldValue").GetValue(item, null);
sbDescription.AppendLine(strDescriptionVals
.Replace("{0}", (propertyName != null) ? propertyName.ToString() : "" + ", "));
sbAllNotes.AppendLine(strAllNotes
.Replace("{0}", (propertyName != null) ? propertyName.ToString() : "")
.Replace("{1}", (newValue != null) ? newValue.ToString() : "")
.Replace("{2}", (oldValue != null) ? oldValue.ToString() : ""));
}
}
}
您可能希望递归地“深入”到items属性中
请注意,只有概念性代码片段,不能保证它在这里编写时正常工作:
void Print(object value, string propertyName)
{
if (property.PropertyType == typeof(string) || property.PropertyType.IsPrimitive)
{
sbAllNotes.AppnedLine(.. propertyName .. value ..);
}
else if ((typeof(IEnumerable).IsAssignableFrom(property.PropertyType)
{
PrintCollectioType((IEnumerable)value);
}
else
{
PrintComplexType(value);
}
}
void PrintCollectioType(IEnumerable collection)
{
foreach (var item in collection)
{
Print(item, "Collection Item");
}
}
void PrintComplexType(IEnumerable collection)
{
foreach(var property in owner.GetType().GetProperties())
{
var propertyName = property.Name;
var propertyValue = property.GetValue(owner);
Print(item, propertyName);
}
}
要打印“道具”,请执行以下操作:
什么是type
variable?@Sybren它的(T)属性在每次方法调用上几乎相同,这就是它是泛型的原因。它是一组非常“匿名”的特性和类。问题是从“集合属性”中检索值。有人有什么想法吗?那么你的集合到底是什么类型的呢?列表、IList、ICollection?您的变量“类型”是什么?这应该是要从中获取属性值的对象实例。