Tag: reflection

如何使用reflection处理数组

我正在写一些validation码。 代码将传递到Web服务中的数据并决定它是否可以执行操作,或者向调用者返回他们错过了某些字段等的消息。 我有它主要工作除了数组。 我使用[RequiredField]属性标记属性以表示所需的字段。 所以如果这是我的一些数据, public enum EnumTest { Value1, Value2 } [DataContract] public class DummyWebserviceData { [DataMember] [RequiredField] public EnumTest[] EnumTest{ get; set; } [DataMember] [RequiredField] public DummyWebserviceData2[] ArrayOfData { get; set; } } [DataContract] public class DummyWebserviceData2 { [DataMember] [RequiredField] public string FirstName { get; set;} [DataMember] [RequiredField] public string LastName { get; […]

找出枚举是否设置了“标志”属性

使用reflection,如何确定枚举是否具有Flags属性 所以对于MyColor返回true [Flags] public enum MyColor { Yellow = 1, Green = 2, Red = 4, Blue = 8 } 并为MyTrade返回false public enum MyTrade { Stock = 1, Floor = 2, Net = 4, }

.NET IL .maxstack指令如何工作?

我想知道.maxstack是如何工作的。 我知道它与您声明的类型的实际大小无关,但与它们的数量有关。 我的问题是: 这仅适用于函数,还适用于我们要求的所有函数? 即使只是函数是.maxstack正在声明,如果你有分支,你怎么知道maxstack是什么? 你去看看所有的“路径”并尽可能返回最大值? 如果我将它设置为16并且实际上有17个变量会发生什么? 如果我把它设置为256,会有太大的惩罚吗?

C#接口inheritance

鉴于: public interface IA { void TestMethod(); } public interface IB : IA { } 为什么: typeof(IB).GetMethods().Count() == 0; ? 要明确一点: public class A { public void TestMethod() { } } public class B : A { } typeof(B).GetMethods().Count(); 确实有效(它返回5); 作为奖励: typeof(IB).BaseType == null

委托任何方法类型 – C#

我想要一个将执行任何外部方法的类,如下所示: class CrazyClass { //other stuff public AnyReturnType Execute(AnyKindOfMethod Method, object[] ParametersForMethod) { //more stuff return Method(ParametersForMethod) //or something like that } } 这可能吗? 是否有代理人采用任何方法签名?

通用静态类 – 在运行时检索对象类型

我有一个X类型的对象,我可以(显然)在运行时检索。 var type = myObject.GetType(); 我有一个通用的静态类。 public static class MyStaticClass { public static void DoStuff(T something) { // bla bla } } 我想做的是: MyStaticClass.DoStuff(myObject); 但我不能。 实际上,MyStaticClass只有几种类型可以运行,并且它们共享多个接口。 一种解决方法是写: if (myObject.GetType() == typeof(X)) { MyStaticClass.DoStuff(myObject as X); } if (myObject.GetType() == typeof(Y)) { MyStaticClass.DoStuff(myObject as Y); } 但它很冗长,写到处都是非常丑陋的 – 我觉得我不应该这样做,但我也不应该这样做。 我无法相信没有解决方案。 或者至少有任何整洁的解决方法? 或者我的方法开始时是错误的(如果是这样的替代方案)? 我应该为X,Y,Z创建一些(抽象?)基类吗?

如何在不知道封闭generics类型的情况下访问通用属性

我有一个通用类型如下 public class TestGeneric { public T Data { get; set; } public TestGeneric(T data) { this.Data = data; } } 如果我现在有一个对象(来自某个外部源),我知道它的类型是一些封闭的TestGeneric ,但我不知道TypeParameter T.现在我需要访问我的对象的数据。 问题是我无法转换对象,因为我不确切知道哪个封闭的TestGeneric。 我用 // thx to http://stackoverflow.com/questions/457676/c-reflection-check-if-a-class-is-derived-from-a-generic-class private static bool IsSubclassOfRawGeneric(Type rawGeneric, Type subclass) { while (subclass != typeof(object)) { var cur = subclass.IsGenericType ? subclass.GetGenericTypeDefinition() : subclass; if (rawGeneric == cur) […]

你如何使用.netreflection与T4?

我有一个包含文本模板的ac #project。 我希望这个模板基于反映项目中的C#类来生成一些SQL。 如何使用T4访问当前项目的内容? 是否可能,如果是,可以使用Reflection,还是只访问必须解析的原始源? 提前致谢!

reflection类获取任何对象的所有属性

我需要创建一个函数来获取对象的所有属性(包括子对象)这是我的错误记录function。 现在我的代码总是返回0个属性。 请让我知道我做错了什么,谢谢! public static string GetAllProperiesOfObject(object thisObject) { string result = string.Empty; try { // get all public static properties of MyClass type PropertyInfo[] propertyInfos; propertyInfos = thisObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Static);//By default, it will return only public properties. // sort properties by name Array.Sort(propertyInfos, (propertyInfo1, propertyInfo2) => propertyInfo1.Name.CompareTo(propertyInfo2.Name)); // write property names StringBuilder sb = […]

解析为Nullable Enum

我试图将字符串解析回MyEnum类型的可空属性。 public MyEnum? MyEnumProperty { get; set; } 我在线收到错误: Enum result = Enum.Parse(t, “One”) as Enum; // Type provided must be an Enum. Parameter name: enumType 我在下面有一个示例控制台测试。 如果我在属性MyEntity.MyEnumProperty上删除nullable,则代码有效。 除了通过reflection之外,如何在不知道typeOf枚举的情况下使代码工作? static void Main(string[] args) { MyEntity e = new MyEntity(); Type type = e.GetType(); PropertyInfo myEnumPropertyInfo = type.GetProperty(“MyEnumProperty”); Type t = myEnumPropertyInfo.PropertyType; Enum result = Enum.Parse(t, […]