Tag: reflection

使用TypeBuilder为基类创建传递构造函数

假设我有一个SpaceShip类,如下所示: public class SpaceShip { public SpaceShip() { } public SpaceShip(IRocketFuelSource fuelSource) { } } 我想使用TypeBuilder在运行时创建一个inheritance自SpaceShip的类型,并为SpaceShip中的每个类型定义一个构造函数。 除了将参数传递给父(“传递”构造函数)之外,我不需要构造函数实际执行任何操作。 例如,如果用C#表示,生成的类型看起来像这样: public class SpaceShipSubClass : SpaceShip { public SpaceShipSubClass() : base() { } public SpaceShipSubClass(IRocketFuelSource fuelSource) : base(fuelSource) { } } 为了使事情复杂化,我实际上并不知道生成类型将inheritance到哪个类直到运行时(因此我将不得不考虑任意数量的构造函数,可能使用默认参数)。 这可能吗? 我想如果我有一个总体方向可以解决这个问题,那就是我对TypeBuilder完全不TypeBuilder 。 谢谢!

从属性构造函数中获取应用了哪个属性的成员?

我有一个自定义属性,在我的自定义属性的构造函数中我想将我的属性的属性值设置为我的属性应用到的属性的类型,是否有某种程度来访问该属性应用于的成员从我的属性类里面?

如何找到两种类型中最小的可分配类型(重复)?

这是两种使用的扩展方法 public static Type FindInterfaceWith(this Type type1, Type type2) { // returns most suitable common implemented interface } public static Type FindBaseClassWith(this Type type1, Type type2) { // returns most derivative of common base class } 如果FindInterfaceWith没有通用的实现接口,则返回null 。 如果FindBaseClassWith没有更多的派生公共基类,则返回System.Object 。 如果其中一个参数是接口,则FindBaseClassWith返回null 。 如果任何参数为null它们都返回null 。 最终解决方案中的方法签名如下: public static Type FindAssignableWith(this Type type1, Type type2) { // […]

C#Reflection – 更改变量字段的值

我有一个类的实例,我想用另一个相同类型的对象(交换)更改此实例的对象数据成员,由于我的系统约束我不能使用=,new或setter运算符。 基本上我想改变一个变量字段的值,该字段是另一个对象中包含的对象 – 我的实例所拥有的变量。 是否有可能使用reflection? 若有人可以请给我基本指示? 谢谢Yoav

StackTrace文件名未知

在我使用StackTrace的代码中发生了一些奇怪的事情。 这几乎就像没有加载调试信息一样……但是我在DEBUG构建中运行它。.pdb文件在bin目录中是最新的并且是最新的。 我已经严重失去了思想: public class TraceHelper { private static IDictionary TraceDictionary = new Dictionary(); public TraceHelper(int duration) { … TraceDictionary[InternalGetCallingLocation()]+=duration; … } public static string InternalGetCallingLocation () { var trace = new System.Diagnostics.StackTrace(); var frames = trace.GetFrames(); var filename = frames[1].GetFileName(); //<<– this always returns null return frames[0].ToString(); //this returns: // "InternalGetCallingLocation at offset 99 […]

确定是否可以将reflection属性指定为null

我希望自动发现有关提供的类的一些信息,以执行类似于表单输入的操作。 具体来说,我使用reflection为每个属性返回PropertyInfo值。 我可以从我的“表单”读取或写入每个属性的值,但如果属性被定义为“int”,我将无法编写空值,我的程序甚至不应该尝试。 如何使用reflection来确定是否可以为给定属性分配空值,而无需编写switch语句来检查每种可能的类型? 特别是我想检测像“int”和“int?”这样的盒装类型之间的区别,因为在第二种情况下我确实希望能够写出一个空值。 IsValueType和IsByRef似乎没有区别。 public class MyClass { // Should tell me I cannot assign a null public int Age {get; set;} public DateTime BirthDate {get; set;} public MyStateEnum State {get; set;} public MyCCStruct CreditCard {get; set;} // Should tell me I can assign a null public DateTime? DateOfDeath {get; set;} public MyFamilyClass […]

使用reflection获取MemberInfo的类型

我正在使用reflection来加载具有项目类结构的树视图。 类中的每个成员都有一个分配给它们的自定义属性。 我使用MemberInfo.GetCustomAttributes()获取类的属性没有问题,但是如果类成员是自定义类,然后需要解析自身以返回自定义属性,我需要一种方法。 到目前为止,我的代码是: MemberInfo[] membersInfo = typeof(Project).GetProperties(); foreach (MemberInfo memberInfo in membersInfo) { foreach (object attribute in memberInfo.GetCustomAttributes(true)) { // Get the custom attribute of the class and store on the treeview if (attribute is ReportAttribute) { if (((ReportAttribute)attribute).FriendlyName.Length > 0) { treeItem.Items.Add(new TreeViewItem() { Header = ((ReportAttribute)attribute).FriendlyName }); } } // PROBLEM HERE […]

类成员的自定义属性

我正在使用自定义属性来定义类的成员如何映射到属性以作为表单发布(支付网关)发布。 我有自定义属性工作正常,并能够通过“名称”获取属性,但希望由成员本身获取属性。 例如: getFieldName(“name”); VS getFieldName(obj.Name); 计划是编写一个方法,将带有成员的类序列化为postable字符串。 这是我此时的测试代码,其中ret是一个字符串,PropertyMapping是自定义属性: foreach (MemberInfo i in (typeof(CustomClass)).GetMember(“Name”)) { foreach (object at in i.GetCustomAttributes(true)) { PropertyMapping map = at as PropertyMapping; if (map != null) { ret += map.FieldName; } } } 提前致谢!

在ViewBag中存储匿名对象

这可能是一个愚蠢的问题,但我试图在ViewBag填充一个匿名对象, ViewBag所示: ViewBag.Stuff = new { Name = “Test”, Email = “user@domain.com” }; 并从这样的视图访问它: @ ViewBag.Stuff.Name 我理解ViewBag是动态的,“Stuff”是一个匿名对象…但是当我从上面的View行看调试器时,我可以看到具有正确值的所有属性。 为什么模型装订器有这么难? 有没有一个很好的方法来实现这一点而不创建模型类? 我想继续使用new {}

将Expression <Func >动态转换为Expression <Func

我找不到从Expression <Func >转换为Expression <Func >的方法。 因为我使用了大量的reflection,实际上,我真正需要的是一种采用类型参数并执行转换的方法。 public object Convert(Expression<Func> expr, Type t); T2来自T1 public class T1 { int FamilyId {get; set;} } public class T2 : T1 { … other properties } 我在基类上定义了一个filter表达式 Expression<Func> filter = p => p.FamilyId == [some value] 我想申请List