C# 使用反射将未知对象强制转换为泛型类
我有一个方法,它把一个对象作为参数。在该方法中,我通过反射遍历对象属性。某些属性属于泛型类类型。我喜欢读取该泛型类属性的属性,但无法将其强制转换为泛型类C# 使用反射将未知对象强制转换为泛型类,c#,generics,reflection,casting,C#,Generics,Reflection,Casting,我有一个方法,它把一个对象作为参数。在该方法中,我通过反射遍历对象属性。某些属性属于泛型类类型。我喜欢读取该泛型类属性的属性,但无法将其强制转换为泛型类 public abstract class BaseClass { public int Id { get; set; } } public abstract class GenericClass<T>: BaseClass { public string Description { get; set; } }
public abstract class BaseClass
{
public int Id { get; set; }
}
public abstract class GenericClass<T>: BaseClass
{
public string Description { get; set; }
}
public class DerivedClassA: GenericClass<DerivedClassA>
{
public string A { get; set; }
}
public class DerivedClassB: GenericClass<DerivedClassB>
{
public string B { get; set; }
}
public class ReflectingClass: BaseClass
{
public string Code { get; set; }
public DerivedClassA DerA { get; set; }
public DerivedClassB DerB { get; set; }
}
public static void Reflecting(object obj)
{
var t = GetType(obj)
foreach (var pi in t.GetProperties())
{
if (obj.GetType().BaseType.GetGenericTypeDefinition() == typeof(GenericClass<>)
{
var genClassObjProperty = ((GenericClass<T>)obj).Description; // Error, cannot do this at all !!!
}
}
}
而不是
BaseClass.DoSomething()
或
BaseClass.DoSomething(类型derivedClassType)
看看这个:
public static void Reflecting(object obj)
{
foreach (var pi in obj.GetType().GetProperties())
{
if (pi.PropertyType.BaseType.IsGenericType
&& pi.PropertyType.BaseType.GetGenericTypeDefinition()
== typeof(GenericClass<>))
{
var propValue = pi.GetValue(obj);
if (propValue != null)
{
var description = propValue.GetType()
.GetProperty("Description").GetValue(propValue);
Console.WriteLine(description);
}
}
}
Console.ReadKey();
}
公共静态空洞反射(对象obj)
{
foreach(obj.GetType().GetProperties()中的var pi)
{
if(pi.PropertyType.BaseType.IsGenericType
&&pi.PropertyType.BaseType.GetGenericTypeDefinition()
==类型(通用类))
{
var propValue=pi.GetValue(obj);
if(propValue!=null)
{
var description=propValue.GetType()
.GetProperty(“说明”).GetValue(propValue);
控制台写入线(说明);
}
}
}
Console.ReadKey();
}
我想这就是你需要的。看看这个:
public static void Reflecting(object obj)
{
foreach (var pi in obj.GetType().GetProperties())
{
if (pi.PropertyType.BaseType.IsGenericType
&& pi.PropertyType.BaseType.GetGenericTypeDefinition()
== typeof(GenericClass<>))
{
var propValue = pi.GetValue(obj);
if (propValue != null)
{
var description = propValue.GetType()
.GetProperty("Description").GetValue(propValue);
Console.WriteLine(description);
}
}
}
Console.ReadKey();
}
公共静态空洞反射(对象obj)
{
foreach(obj.GetType().GetProperties()中的var pi)
{
if(pi.PropertyType.BaseType.IsGenericType
&&pi.PropertyType.BaseType.GetGenericTypeDefinition()
==类型(通用类))
{
var propValue=pi.GetValue(obj);
if(propValue!=null)
{
var description=propValue.GetType()
.GetProperty(“说明”).GetValue(propValue);
控制台写入线(说明);
}
}
}
Console.ReadKey();
}
我想这就是你需要的。我完全不知道你想做什么,也不知道为什么这里需要泛型。看起来是XY。为什么要尝试使用反射来访问属性?更大的图景是什么?试图使示例尽可能基本,但显然太基本了:我在基类中使用反射来记录日志,它不知道它得到的是哪种类型的对象。我为什么使用泛型是在上面的帖子中提到的。为日志目的做反射这样昂贵的事情对我来说似乎有点过头了。您使用的是日志框架吗?NLog、log4net、Serilog、Microsoft日志?
GetType
返回什么?这是一种静态方法吗?我完全不知道你想做什么,也不知道为什么这里需要泛型。为什么要尝试使用反射来访问属性?更大的图景是什么?试图使示例尽可能基本,但显然太基本了:我在基类中使用反射来记录日志,它不知道它得到的是哪种类型的对象。我为什么使用泛型是在上面的帖子中提到的。为日志目的做反射这样昂贵的事情对我来说似乎有点过头了。您使用的是日志框架吗?NLog、log4net、Serilog、Microsoft日志?GetType
返回什么?是静态的方法吗?是的,非常感谢!你让我开心。这正是我需要的。我从未想过也要反映泛型类的属性。太棒了,再次感谢!我希望我能以一千分的优势投票给你,但遗憾的是,我仍然处于能够投票的门槛之下。是的,非常感谢你!你让我开心。这正是我需要的。我从未想过也要反映泛型类的属性。太棒了,再次感谢!我希望我能以一千分的优势投票给你,但遗憾的是,我仍然无法投票给你。