fieldinfo

How to reference a field by reflection

时光总嘲笑我的痴心妄想 提交于 2019-12-22 17:46:10
问题 Sorry for the title, it's not explicit. Further to my precedent question, I want to subscribe a method to an event object retrieved dynamically (via reflection). The object in question is a field of a Control : public void SubscribeEvents(Control control) { Type controlType = control.GetType(); FieldInfo[] fields = controlType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); MethodInfo method = typeof(Trace).GetMethod("WriteTrace"); // "button1" hardcoded for

C# Developing .Net3.5 using reflection to get/set values to nested properties and/or nested fields

六月ゝ 毕业季﹏ 提交于 2019-12-18 09:37:04
问题 I'm developing an app that works with data block classes inheritied from a base class and I am trying to use Reflection to drill down to properties/fields in my data block class. Since all the data block classes are derived/inherited from the base class (which contains a Size property) I can use a general variable of type base class to create an object in my app easily enough; I can also get/set properties at a top level. My problem occurs when the property is a field - I do not know how to

How to get the FieldInfo of a field from the value

我是研究僧i 提交于 2019-12-12 03:08:18
问题 I want to access the FieldInfo, for the CustomAttributes that are on a field, and other purposes, but I'd prefer not to use a string to access that field, nor to have to run through all the fields in a class. If I simply have, class MyClass { #pragma warning disable 0414, 0612, 0618, 0649 private int myInt; #pragma warning restore 0414, 0612, 0618, 0649 public MyClass() { BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; Console.WriteLine( GetType()

C# FieldInfo reflection alternatives

喜欢而已 提交于 2019-12-08 09:48:06
问题 I am currently using FieldInfo.GetValue and FieldInfo.SetValue quite a lot in my programm, which is significantly slowing up my programm. For PropertyInfo I use the GetValueGetter and GetValueSetter methods so I only use reflection once for a given type. For the FieldInfo , the methods don't exist. What is the suggested approach for FieldInfo ? EDIT: I followed this useful link from CodeCaster's reply. This is a great search direction. Now the "only" point that I don't get in this answer is

How to reference a field by reflection

 ̄綄美尐妖づ 提交于 2019-12-06 09:20:15
Sorry for the title, it's not explicit. Further to my precedent question , I want to subscribe a method to an event object retrieved dynamically (via reflection). The object in question is a field of a Control : public void SubscribeEvents(Control control) { Type controlType = control.GetType(); FieldInfo[] fields = controlType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); MethodInfo method = typeof(Trace).GetMethod("WriteTrace"); // "button1" hardcoded for the sample FieldInfo f = controlType.GetField("button1", BindingFlags.Public | BindingFlags.NonPublic |

Get line numbers of fields without using a c# parser

让人想犯罪 __ 提交于 2019-12-05 01:06:52
问题 I would like to get the line #s of a type's fields. To get the line #'s of the statements in a method it is simple enough: Type type = typeof(MyClass); MethodInfo methodInfo = type.GetMethod("SomeMethod"); int token = methodInfo.MetadataToken; ISymbolReader reader = SymUtil.GetSymbolReaderForFile(@"dllName", null); // from mike stall's pdb2xml ISymbolMethod methodSymbol = reader.GetMethod(new SymbolToken(token)); int count = methodSymbol.SequencePointCount; ISymbolDocument[] docs = new

Get line numbers of fields without using a c# parser

半世苍凉 提交于 2019-12-03 16:36:59
I would like to get the line #s of a type's fields. To get the line #'s of the statements in a method it is simple enough: Type type = typeof(MyClass); MethodInfo methodInfo = type.GetMethod("SomeMethod"); int token = methodInfo.MetadataToken; ISymbolReader reader = SymUtil.GetSymbolReaderForFile(@"dllName", null); // from mike stall's pdb2xml ISymbolMethod methodSymbol = reader.GetMethod(new SymbolToken(token)); int count = methodSymbol.SequencePointCount; ISymbolDocument[] docs = new ISymbolDocument[count]; int[] startColumn = new int[count]; int[] endColumn = new int[count]; int[] startRow

Why is a member of base class different from the same member in derived class?

不羁岁月 提交于 2019-12-01 20:54:20
问题 This is a followup to this question: Lambda expression not returning expected MemberInfo class Human { public string name { get; set; } } class Man : Human { } var m1 = typeof(Human).GetProperty("name"); var m2 = typeof(Man).GetProperty("name"); //m1 != m2 why? The same holds for MethodInfo s. I can understand there has to be a difference when Human is an interface, or when name of Human is abstract/virtual. But why is it so for sealed types? Isn't name of Man exactly name of Human ?

C# Developing .Net3.5 using reflection to get/set values to nested properties and/or nested fields

霸气de小男生 提交于 2019-11-29 17:14:49
I'm developing an app that works with data block classes inheritied from a base class and I am trying to use Reflection to drill down to properties/fields in my data block class. Since all the data block classes are derived/inherited from the base class (which contains a Size property) I can use a general variable of type base class to create an object in my app easily enough; I can also get/set properties at a top level. My problem occurs when the property is a field - I do not know how to get to the next level in the field to get to the base properties and/or fields, if applicable. My

Getting the attributes of a field using reflection in C#

隐身守侯 提交于 2019-11-28 09:08:09
问题 I wrote a method that extracts fields from an object like this: private static string GetHTMLStatic(ref Object objectX, ref List<string> ExludeFields) { Type objectType = objectX.GetType(); FieldInfo[] fieldInfo = objectType.GetFields(); foreach (FieldInfo field in fieldInfo) { if(!ExludeFields.Contains(field.Name)) { DisplayOutput += GetHTMLAttributes(field); } } return DisplayOutput; } Each field in my class also has it's own attributes, in this case my attribute is called HTMLAttributes.