Tag: reflection

在C#中使用reflection来获取嵌套对象的属性

鉴于以下对象: public class Customer { public String Name { get; set; } public String Address { get; set; } } public class Invoice { public String ID { get; set; } public DateTime Date { get; set; } public Customer BillTo { get; set; } } 我想使用reflection来通过Invoice获取Customer的Name属性。 以下是我之后,假设此代码将工作: Invoice inv = GetDesiredInvoice(); // magic method […]

检测一个方法是否被reflection(C#)覆盖

假设我有一个基类TestBase,我定义了一个虚方法TestMe() class TestBase { public virtual bool TestMe() { } } 现在我inheritance这个类: class Test1 : TestBase { public override bool TestMe() {} } 现在,使用reflection,我需要find如果方法TestMe已被覆盖子类 – 是否有可能? 我所需要的 – 我正在编写一个types为“object”的devise器可视化工具来显示inheritance的整个层次结构,并显示哪个虚拟方法在哪个级别被覆盖。

快速创build对象而不是Activator.CreateInstance(type)

我试图改善我们的应用程序的性能。 我们有很多Activator.CreateInstance调用导致一些悲伤。 我们基于一个接口(ITabDocument)来实例化很多类,然后环顾四周,我想到了使用这个代码: 代码不会比使用我们拥有的Activator.CreateInstance代码更好。 public static Func<T> CreateInstance<T>(Type objType) where T : class, new() { var dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + objType.Name, objType, null, objType); ILGenerator ilGen = dynMethod.GetILGenerator(); ilGen.Emit(OpCodes.Newobj, objType.GetConstructor(Type.EmptyTypes)); ilGen.Emit(OpCodes.Ret); return (Func<T>)dynMethod.CreateDelegate(typeof(Func<T>)); } 我想知道为什么这是我所做的是: ITabDocument document = CreateInstance<ITabDocument>(Type.GetType("[Company].Something")); 有没有更好的方法来创build可以协助上述的对象? 当你不确定具体types时有点难。

是否有可能使用Javareflection创build一个嵌套类的实例?

代码示例: public class Foo { public class Bar { public void printMesg(String body) { System.out.println(body); } } public static void main(String[] args) { // Creating new instance of 'Bar' using Class.forname – how? } } 有没有可能创build一个名为Bar的新实例? 我试着用: Class c = Class.forName("Foo$Bar") 它find类,但是当我使用c.newInstance()它会引发InstantiationException。

使用lambdaexpression式获取属性名称和types

我正在尝试编写一个函数,使用如下所示的语法来提取属性的名称和types: private class SomeClass { Public string Col1; } PropertyMapper<Somewhere> propertyMapper = new PropertyMapper<Somewhere>(); propertyMapper.MapProperty(x => x.Col1) 有什么办法可以将这个属性传递给函数,而不会对这个语法做任何重大的修改? 我想获得属性名称和属性types。 所以在下面的例子中,我想要检索 Name = "Col1"和Type = "System.String" 谁能帮忙?

从类中打印所有variables值

我有一个有关Person的信息的类,看起来像这样: public class Contact { private String name; private String location; private String address; private String email; private String phone; private String fax; public String toString() { // Something here } // Getters and setters. } 我想toString()返回this.name +" – "+ this.locations + …为所有variables。 我试图用这个问题所示的reflection来实现它,但我无法打印实例variables。 什么是解决这个问题的正确方法?

为什么要检查这个!= null?

偶尔,我想花一些时间看.NET代码,看看事情是如何在幕后实现的。 我偶然发现了这个gem,同时通过Reflector查看String.Equals方法。 C# [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public override bool Equals(object obj) { string strB = obj as string; if ((strB == null) && (this != null)) { return false; } return EqualsHelper(this, strB); } IL .method public hidebysig virtual instance bool Equals(object obj) cil managed { .custom instance void System.Runtime.ConstrainedExecution.ReliabilityContractAttribute::.ctor(valuetype System.Runtime.ConstrainedExecution.Consistency, valuetype System.Runtime.ConstrainedExecution.Cer) = { int32(3) […]

在unit testing中使用reflection是不好的做法?

在过去的几年中,我一直认为在Java中,reflection在unit testing中被广泛使用。 由于一些必须检查的variables/方法是私人的,因此需要读取它们的值。 我一直认为Reflection API也用于这个目的。 上周我不得不testing一些包,因此写了一些JUnittesting。 和往常一样,我使用Reflection来访问私有字段和方法。 但是,我的主pipe检查了代码并不是很满意,并告诉我Reflection API不是用来做这种“黑客行为”的。 相反,他build议修改生产代码中的可视性。 使用reflection真的是不好的做法吗? 我不能相信 – 编辑:我应该提到,我被要求,所有的testing都在一个单独的包称为testing(所以使用受保护的visibilty,例如也不是一个可能的解决scheme)

从IEnumerable <T>获得typesT.

有没有办法通过reflection从IEnumerable<T>检索typesT ? 例如 我有一个variablesIEnumerable<Child>信息; 我想通过reflection来检索孩子的types

Convert.ChangeType()在可空types上失败

我想将string转换为对象属性值,其名称作为string。 我正在尝试这样做: string modelProperty = "Some Property Name"; string value = "SomeValue"; var property = entity.GetType().GetProperty(modelProperty); if (property != null) { property.SetValue(entity, Convert.ChangeType(value, property.PropertyType), null); } 问题是这是失败,并且当属性types是可为空的types时抛出一个无效的转换exception。 这不是值不能被转换的情况下 – 他们将工作,如果我手动(例如DateTime? d = Convert.ToDateTime(value); )我已经看到一些类似的问题,但仍然无法得到它上class。