Tag: reflection

派生和基类,我可以明确设置基数吗?

public class SuperCar: Car { public bool SuperWheels { get {return true; } } } public class Car { public bool HasSteeringWheel { get {return true;} } } 如何设置派生Supercar的基类? 例如,我想简单地设置SuperCars基类,如下所示: public void SetCar( Car car ) { SuperCar scar = new SuperCar(); car.Base = car; } 基本上,如果我有Car对象,我不想手动迭代汽车的每个属性以设置SuperCar对象,我认为这是你可以做到的唯一方法,但如果你可以用另一种方式做到这一点会好得多。

C#Reflection – 对象与目标类型不匹配

我正在尝试使用propertyInfo.SetValue()方法使用reflection设置对象属性值,并且我得到exception“对象与目标类型不匹配”。 它实际上没有意义(至少对我来说!)因为我只是试图在一个带有字符串替换值的对象上设置一个简单的字符串属性。 这是一个代码片段 – 它包含在一个递归函数中,所以有更多的代码,但这是胆量: PropertyInfo fieldPropertyInfo = businessObject.GetType().GetProperties().FirstOrDefault(f => f.Name.ToLower() == piecesLeft[0].ToLower()); businessObject = fieldPropertyInfo.GetValue(businessObject, null); fieldPropertyInfo.SetValue(businessObject, replacementValue, null); 我已经通过执行此比较validation了“businessObject”和“replacementValue”都是相同的类型,返回true: businessObject.GetType() == replacementValue.GetType()

reflection过程中出现“找不到属性集方法”错误

我试图反映一些类属性并以编程方式设置它们,但看起来我的PropertyInfofilter之一无法正常工作: //Get all public or private non-static properties declared in this class (no inherited properties) – that have a getter and setter. PropertyInfo[] props = this.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.SetProperty ); 我收到了错误 pi.SetValue(this, valueFromData, null); 因为该属性只有get{}方法,所以没有set{}方法。 我的问题是,为什么这个属性没有过滤掉道具? 我认为那是BindingFlags.SetProperty的目的。 未过滤掉的属性是: public String CollTypeDescription { get { return _CollTypeDescription; } } […]

使用.NET Core中的字符串(配置文件)进行ServiceCollection配置

有没有办法在.net核心的标准Microsoft.Extensions.DependencyInjection.ServiceCollection库中配置dependency injection,而实际上没有对相关实现类的引用? (从配置文件中获取实现类名?) 例如: services.AddTransient(“The.Actual.Thing”);// Where The.Actual.Thing is a concrete class

使用reflection调用generics重载方法

我需要使用reflection来调用重载方法。 我的课程如下: public static Transformer { //Overloaded method with generics parameter. First Transform Method public static TranformerResult Transform(object [] entities, List dataContract) where T:class { return transformerResult; } //Overloaded method without generics parameter. Second Transform Method public static TranformerResult Transform(object entities, Type dataContract) { return transformerResult; } } public class TransformerResult { public List GetTypes() […]

MethodInfo.Invoke参数顺序

我正在尝试使用reflection来调用方法。 像这样的东西: method.Invoke(instance, propValues.ToArray()) 问题是没有办法确保参数数组的顺序正确。 有没有办法具体说明哪个值依赖于哪个参数? 或者我真的需要制作自定义活页夹吗? 如果是这样,有人能引导我朝正确的方向发展吗?

获取列表中属性的名称

例 public class MyItems { public object Test1 {get ; set; } public object Test2 {get ; set; } public object Test3 {get ; set; } public object Test4 {get ; set; } public List itemList { get { return new List { Test1,Test2,Test3,Test4 } } } } public void LoadItems() { foreach (var item […]

调用MethodInfo.MakeGenericMethod时,此键错误是什么意思?

今天我从一些旧的动态转换代码中得到了这个错误(我已经改变了最后一行并省略了堆栈跟踪的其余部分): Item has already been added. Key in dictionary: ‘Int32 Count[Object](System.Collections.Generic.IEnumerable`1[System.Object])’ Key being added: ‘Int32 Count[Object](System.Collections.Generic.IEnumerable`1[System.Object])’ —> System.ArgumentException: Item has already been added. Key in dictionary: ‘Int32 Count[Object](System.Collections.Generic.IEnumerable`1[System.Object])’ Key being added: ‘Int32 Count[Object](System.Collections.Generic.IEnumerable`1[System.Object])’ at System.Reflection.CerHashtable`2.Insert(K[] keys, V[] values, Int32& count, K key, V value) at System.Reflection.CerHashtable`2.Preallocate(Int32 count) at System.RuntimeType.RuntimeTypeCache.GetGenericMethodInfo(RuntimeMethodHandle genericMethod) at System.RuntimeType.GetMethodBase(RuntimeTypeHandle reflectedTypeHandle, RuntimeMethodHandle methodHandle) […]

如何在/ .Net中拦截来自/到第三方库的非虚拟方法的调用?

我认为我需要的是.net人称之为“透明动态代理”的东西,但我迄今为止看到的所有实现(Castle DynamicProxy,Spring.NET AOP等)都要求我至少执行以下其中一项: 将截获的方法声明为虚拟 包装类并创建包装器的实例而不是包装类 更改inheritance或实现接口 显然,如果调用者和被调用者都是非虚拟的,并且来自第三方封闭源库,那么我就无能为力。 如果C#是像Python这样的动态语言,我会这样做: foo = ThirdyPartyLibA.Foo() def interceptor(self, *args, **kwargs): do_something_before(self, *args, **kwargs) result = ThirdyPartyLibB.Bar.intercepted(self, *args, **kwargs) do_something_after(self, result, *args, **kwargs) return result foo.bar.intercepted = interceptor # bar is an instance of ThirdyPartyLibB.Bar foo.do_its_job() # Foo.do_its_job calls Bar.intercepted 我需要这个来改变ThirdyPartyLibA.Foo的不良行为,同时与ThirdyPartyLibB.Bar交互。 我确切知道导致这种行为的原因以及如何更改Foo或Bar来修复此错误,这要归功于dissasemblers。 一些(非常不可能工作)的想法: 反汇编ThirdyPartyLibA,在代码中进行更改并生成兼容的程序集(不太可能工作,因为它是一个强命名的程序集) 编辑二进制文件以使Foo的错误方法成为虚拟的,并改变它保持有效程序集所需的一切,这样我就可以使用动态代理(不太可能工作,也因为与上述想法相同的原因) 找到一个适合的透明动态代理实现(我认为没有基于这个论坛post: http : //www.pcreview.co.uk/forums/overriding-non-virtual-methods-using-il-and-reflection- emit-t2605695.html ) […]

C#generics类使用引用类型和可空值类型

我有一个有趣的问题。 我想创建一个可以处理Reference类型和Nullable类型的generics类。 基本上我想要的东西: public class ClassWithNull { public T varName = null; } 当然,这当然不会编译,因为并非所有类型都可以赋值为null,即不可为空的值类型。 但问题是Nullable是一个值类型,所以简单地添加where T : class对我没有帮助。 我的genericsfoo不是太强,但是我无法找到任何方式来说T必须是引用类型或可以为值的类型。 我必须解决的想法是使ClassWithNull成为一个抽象类。 然后我可以添加两个子类,一个用于处理引用类型,另一个用于处理可空值类型。 然后,基类中的静态工厂方法可以使用reflection来确定应该构造哪个子类。 就像是: public static ClassWithNull CreateClassWithNull() { StackTrace st = new StackTrace(); Type type = st.GetFrame(1).GetMethod().GetGenericArguments()[0]; if (!type.IsValueType) { return new ClassWithReferenceType(); } else if (type == typeof(Nullable)) { return new ClassWithNullableValueType(); } else […]