Tag: reflection

如何用reflection读取方法体

是否有可能找到有关reflection的Method主体的任何信息? 怎么样?

在业务逻辑中使用reflection是一种好习惯吗?

我需要处理一个由两个主要部分组成的应用程序: 具有特定业务类的业务逻辑部分(例如,书籍,库,作者……) 可以在数据网格中显示Books,Libraries,…的通用部件,将它们映射到数据库,……)。 通用部分使用reflection从业务类中获取数据,而无需在业务类中编写特定的数据网格或数据库逻辑。 这很好,允许我们添加新的业务类(例如LibraryMember),而无需调整数据网格和数据库逻辑。 但是,多年来,代码被添加到业务类中,这些业务类还使用reflection来在业务类中完成工作。 例如,如果书籍的作者被更改,观察者会被要求告诉作者本身它应该将这本书添加到他所写的书籍集(Author.Books)中。 在这些观察者中,不仅传递实例,而且还传递直接从reflection得到的信息(FieldInfo被添加到观察者调用,以便调用者知道书的字段“作者”被更改)。 我可以清楚地看到在这些通用模块(如数据网格或数据库接口)中使用reflection的优势,但在我看来,在业务类中使用reflection是一个坏主意。 毕竟,应用程序不应该尽可能不依赖于reflection吗? 或者反思是否是21世纪“正常的工作方式”? 在业务逻辑中使用reflection是一种好习惯吗? 编辑 :关于Kirk的评论的一些澄清: 想象一下,作者在Book上实现了一个观察者。 只要书籍的某些字段发生变化(如标题,年份,#页面,作者……),书籍就会调用所有观察者。 更改字段的“FieldInfo”在观察者中传递。 然后,作者 – 观察者使用此FieldInfo来决定是否对此更改感兴趣。 在这种情况下,如果FieldInfo用于Book of Book的作者,则Author-Observer将更新其自己的Books向量。

将“C#friendly type”转换为实际类型:“int”=> typeof(int)

我希望得到一个System.Type给定一个string ,该string指定(原始)类型的C#友好名称 ,基本上就像C#编译器在读取C#源代码时所做的那样。 我觉得描述我所追求的是以unit testingforms出现的最佳方式。 我希望存在一种通用技术可以使所有下面的断言通过,而不是试图对特殊C#名称的特殊情况进行硬编码。 Type GetFriendlyType(string typeName){ …??… } void Test(){ // using fluent assertions GetFriendlyType( “bool” ).Should().Be( typeof(bool) ); GetFriendlyType( “int” ).Should().Be( typeof(int) ); // ok, technically not a primitive type… (rolls eyes) GetFriendlyType( “string” ).Should().Be( typeof(string) ); // fine, I give up! // I want all C# type-aliases to work, not […]

不区分大小写的GetMethod?

foreach(var filter in filters) { var filterType = typeof(Filters); var method = filterType.GetMethod(filter); if (method != null) value = (string)method.Invoke(null, new[] { value }); } 是否有一种不区分大小写的方法来获取方法?

将对象转换为IEnumerable ,其中T未知

我正在尝试使用Reflection并遇到以下情况。 在下面的代码中,我们假设’obj’可以是IEnumerable或ICollection或IList 。 我想将此System.Object强制转换为IEnumerable always(因为ICollection和IList IEnumerable仍然从IEnumerableinheritance),所以我想枚举集合并使用reflection来编写单个项目。 这背后的动机是我只是想看看Serializers一般会如何序列化数据,所以我试图模拟这种情况,希望也能理解Reflection。 我想把对象转换为非通用的IEnumerable ,但是认为这会导致不必要的对象装箱,当我们说IEnumerable的实际实例时……我在想什么? private void WriteGenericCollection(object obj) { Type innerType = obj.GetType().GetGenericArguments()[0]; //Example: IEnumerable or IEnumerable Type generatedType = typeof(IEnumerable).MakeGenericType(innerType); //how could i enumerate over the individual items? }

盒装价值类型比较

我在这里想要实现的是盒装基元类型的直接值比较。 ((object)12).Equals((object)12); // Type match will result in a value comparison, ((object)12).Equals((object)12d); // but a type mismatch will not. (false) object.Equals((object)12,(object)12d); // Same here. (false) 我理解’为什么’。 我只是没有看到’怎么样’。 这些类型在运行时之前是未知的,它们可以是来自数据源的任何基本类型。 这包括字符串,日期时间,bool等等。我已经走下了编写扩展方法的丑陋路线,该方法解决了两种类型,然后在进行’=​​=’比较之前进行转换:(为了完整性,我包括了每种基本类型,加上我感兴趣的那些) public static bool ValueEquals(this object thisObj, object compare) { if (thisObj is int) { int obj = (int)thisObj; if (compare is int) return (obj == (int)compare); […]

TypeDescriptor.GetConverter()不会返回我的转换器

我有一个简单的类型与自定义类型转换器,在运行时编译和加载。 但是,TypeDescriptor.GetConverter()找不到合适的转换器。 这是一个独立的例子 using System; using System.ComponentModel; using System.Collections.Generic; using System.CodeDom.Compiler; using Microsoft.CSharp; public class Program { private static string src = @” using System; using System.ComponentModel; namespace LoadMe { [TypeConverter(typeof(FooConverter))] public class Foo { } public class FooConverter : TypeConverter { // stuff } } “; public static void Main() { var codeProvider = […]

在.NET中,您可以使用reflection来获取类的所有非inheritance方法吗?

由于这里的问题,我正在尝试编写一个自定义JsonConverter来处理您对列表或集合进行子类化的情况,然后为其添加额外的属性。 因此,一种方法是忽略所有基类属性,并仅序列化已定义类中的属性。 (从技术上讲,这是行不通的,因为如果你inheritance了那个子类,你就会破坏序列化,但它确实让我想知道……) 是否有可能通过reflection(我知道答案是’是’,因为Reflector确实如此,但我不知道如何)只获得在类本身定义的成员而不是inheritance的成员? 例如… public class MyBaseClass { public string BaseProp1 { get; set; } public string BaseProp2 { get; set; } } public class MySubClass : MyBaseClass { public string SubProp1 { get; set; } public string SubProp2 { get; set; } } 在这种情况下,我想反思MySubClass并且只忽略BaseProp1和BaseProp2而得到SubProp1和SubProp2 。 那怎么可能呢? 中号

属性上的reflection和访问修饰符

我编写了一些代码来查看使用reflection的属性。 我使用reflection从类中检索了一个属性列表。 但是,我需要找出该物业是公共的还是受保护的。 例如: public string Name{get;set;} protected int Age{get;set;} PropertyInfo类似乎不公开有关该属性的此信息。 还有另一种方法吗?

如何隐式地reflection方法调用

我有一个可以从string隐式转换的类Thing 。 当我直接使用Thing参数调用方法时,从string到Thing的强制转换正确完成。 但是,如果我使用reflection调用相同的方法,它会抛出exception System.ArgumentException : Object of type ‘System.String’ cannot be converted to type ‘Things.Program+Thing’. 也许有充分的理由,但我无法弄明白。 有人知道如何使用reflection工作吗? namespace Things { class Program { public class Thing { public string Some; public static implicit operator Thing(string s) { return new Thing {Some = s}; } } public void showThing(Thing t) { Console.WriteLine(“Some = ” + […]