Tag: reflection

C#reflection:获取类和基类的所有成员的信息

当我运行以下代码时,它只返回一个MethodInfo / FieldInfo / FieldInfo 它直接属于我正在搜索信息对象的Type 。如何查找信息对象,而不管它是否存在于基类中并且可以是私有的? obj.GetType().GetMethod(methodName, bindingFlags);

将本地语言exception转换为英语exception,包括英语堆栈跟踪

Currentlly我正在使用这种方法将exception转换为将原始exception消息转换为英语的exception: C#: [DebuggerStepThrough()] [Extension()] public Exception ToEnglish(T ex) where T : Exception { CultureInfo oldCI = Thread.CurrentThread.CurrentUICulture; Exception exEng = default(Exception); Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(“en-US”); // Instance Exception one time to retrieve an English message. exEng = (Exception)Activator.CreateInstance(ex.GetType); // Instance Exception a second time to pass additional parameters to its constructor. exEng = (Exception)Activator.CreateInstance(ex.GetType, new object[] […]

通用方法中的Cast接口

我是界面的新用户。 我的问题是我创建了一个Generic类,它有两个参数,一个是对象类型,另一个是接口类型。 现在在类中我可以使用Reflection来转换对象,但我没有获得构建接口的方法。

例外与反思

Web服务具有SoapExtension,它包含error handling程序和xml格式的序列化错误。 Exception text. 如何制作error handling程序,调用“类型”错误? 例如: Type _type = Type.GetType(doc.DocumentElement.Attributes[“Type”].Value); 必须调用NullReferenceException。

c#插件(reflection)

我在使用reflection创建插件解决方案时遇到了麻烦。 当我点击menuItem时,我想加载并在我的窗口中显示另一个程序。 我想我需要3个项目。 单击menuItem时想要加载另一个程序的客户端程序 我要加载的程序 插件类 代码客户端程序 private void nQueensToolStripMenuItem_Click(object sender, EventArgs e) { // Create an assembly object to load our classes string path = Application.StartupPath + “\\NQueens.dll”; Assembly ass = Assembly.LoadFile(path); Type objType = ass.GetType(“NQueens.NQueen”); // Create an instace of NQueens.NQueen var instance = Activator.CreateInstance(objType); // public static bool berekenQueens() objType.InvokeMember(“berekenQueens”, BindingFlags.InvokeMethod | […]

FastMember列顺序保存

使用TypeAccessor.Create FastMember时,似乎总是按字母顺序返回列的列表。 是否可以告诉它保留类中列的顺序? 例如: var testClass = new { B = “1”, A = “2” }; 将从GetMembers返回A列,然后返回B,我想让它保留B的排序,然后是A,如果可能的话。

根据c#中的用户输入解析为原始类型

我这样做的代码使用我给它的reflection和字符串,而不是用户输入。 最终我希望用户能够说“浮动”“2.0”并让计算机说,是的,这是一个浮动,或计算机会说的“bool”“abc”,这是没有听说过的布尔值。 获取用户输入并将其转换为基本类型名称是很简单的,例如“string”到“System.String”,“float”到“System.Single”等等(尽管如果你知道一个函数要做到这一点,那也会很棒。) 这是代码: Console.WriteLine(“1.0 => {0}”, System.Single.Parse(“1.0”)); // this works fine. Type t = Type.GetType(“System.Single”); // for parsing floats MethodInfo mi = t.GetMethod(“System.Single.Parse”); // “ambiguous” if use “Parse” object[] parameters = new object[] { “1.0” }; float f = (float)(mi.Invoke(null, parameters)); // get null exception here. Console.WriteLine(“Was succesfully parsed to: ” + f); 但是我在第二行到最后一行继续得到一个空例外。 […]

为什么在动态创建事件处理程序时会出现Argumentexception?

美好的一天 我写了以下方法: private void RegisterEvent(object targetObject, string eventName, string methodName) { EventInfo eventInfo = targetObject.GetType().GetEvent(eventName); MethodInfo method = eventInfo.EventHandlerType.GetMethod(“Invoke”); IEnumerable types = method.GetParameters().Select(param => param.ParameterType); DynamicMethod dynamicMethod = new DynamicMethod(eventInfo.EventHandlerType.Name, typeof (void), types.ToArray(), typeof (QueryWindow)); MethodInfo methodInfo = typeof (QueryWindow).GetMethod(methodName, new[] { typeof (object) }); ILGenerator ilGenerator = dynamicMethod.GetILGenerator(256); ilGenerator.Emit(OpCodes.Ldarg_1); ilGenerator.EmitCall(OpCodes.Call, methodInfo, null); dynamicMethod.DefineParameter(1, ParameterAttributes.In, […]

使用构造函数中的reflection设置数据类的属性

我有几个以类似于下面的方式定义的数据类,我正在尝试决定是否为每个数据类都有一个内置的构造函数来填充成员,或者在调用方法中只使用一次reflection: public class reportData { public List Deposits; } public class Deposits { public Deposits(List<Dictionary> LPQReq) { PropertyInfo[] properties = typeof(Deposits).GetProperties(); foreach (Dictionary PQList in LPQReq) { foreach (KeyValuePair kvp in PQList) { MemberInfo info = typeof(Deposits).GetField(kvp.Key) as MemberInfo ?? typeof(Deposits).GetProperty(kvp.Key) as MemberInfo; //PropertyInfo property = properties[kvp.Key]; // info.setvalue } } foreach (PropertyInfo property in […]

使用reflection嵌套的完全限定属性名称

我有以下课程: public class Car { public Engine Engine { get; set; } public int Year { get; set; } } public class Engine { public int HorsePower { get; set; } public int Torque { get; set; } } 我使用这个得到所有嵌套属性: var result = typeof(Car).GetProperties(BindingFlags.Public | BindingFlags.Instance).SelectMany(GetProperties).ToList(); private static IEnumerable GetProperties(PropertyInfo propertyInfo) { if (propertyInfo.PropertyType.IsClass) { […]