Questions tagged [fieldinfo]

54 questions
32
votes
8 answers

Is there a way to create a delegate to get and set values for a FieldInfo?

For properties there are GetGetMethod and GetSetMethod so that I can do: Getter = (Func)Delegate.CreateDelegate(typeof(Func), propertyInfo.GetGetMethod()); and Setter = (Action
nawfal
  • 70,104
  • 56
  • 326
  • 368
26
votes
2 answers

How to find if a member variable is readonly?

class Bla { public readonly int sum; } FieldInfo f = type.GetField("sum"); f.?? // what? How do I find if sum is readonly or not? For properties I can do PropertyInfo.CanWrite to find if the member has write access.
nawfal
  • 70,104
  • 56
  • 326
  • 368
8
votes
1 answer

Field getter/setter with expression tree in base class

Following the examples on this post and its follow-up question, I am trying to create field getters / setters using compiled expressions. The getter works just great, but I am stuck the setter, as I need the setter to assign any type of fields. Here…
neggenbe
  • 1,697
  • 2
  • 24
  • 62
7
votes
1 answer

What are GetField, SetField, GetProperty and SetProperty in BindingFlags enumeration?

I have no idea what these are for. The documentation is not very clear: GetField Specifies that the value of the specified field should be returned. SetField Specifies that the value of the specified field should be set. GetProperty Specifies that…
nawfal
  • 70,104
  • 56
  • 326
  • 368
6
votes
1 answer

C# Reflection: What is the difference between FieldInfo.SetValue() and FieldInfo.SetValueDirect()?

The FieldInfo documentation I was able to find did not compare and contrast these two methods. I am interested in understanding when one should be used over the other (preferably with a small code snippet).
Jim
  • 869
  • 1
  • 10
  • 16
6
votes
2 answers

C# reflection GetValue from a field in generic base class

the problem is that we cannot GetValue of a field (non generic) that only resides in base class that has generic type. please see the code snippet below. calling f.GetValue(a) will throw the exception with message: Late bound operations cannot be…
user1519274
  • 61
  • 1
  • 3
4
votes
0 answers

Get line numbers of fields without using a c# parser

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 =…
user420667
  • 6,552
  • 15
  • 51
  • 83
4
votes
1 answer

FieldInfo.GetValue return null for a private member while debugger indicates field is non null?

In C# / .NET 4.0 I am trying to retrieve a field value through reflection with: var bar = foo.GetType() .GetField("_myField", BindingFlags.Instance | BindingFlags.NonPublic) .GetValue(foo) I am a bit puzzled by the situation. The value returned…
Joannes Vermorel
  • 8,976
  • 12
  • 64
  • 104
4
votes
1 answer

How to reference a field by reflection

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…
Florian
  • 4,507
  • 10
  • 53
  • 73
4
votes
1 answer

Why is MemberInfo.GetCustomAttributes(Type) defined to return an array of attributes?

public enum Animal { [Description("King of jungle")] Lion= 1, [Description("Tallest there")] Giraffe = 2 } Suppose I have the FieldInfo, I can go about it two ways: //static one on 'Attribute' class Attribute attribute =…
nawfal
  • 70,104
  • 56
  • 326
  • 368
4
votes
1 answer

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

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 =…
nawfal
  • 70,104
  • 56
  • 326
  • 368
4
votes
3 answers

How do I get the FieldInfo of an array field?

I'm trying to get the field info of an array value from within a struct. So far I have the following, but I dont see how to get the infomration I want. [StructLayout(LayoutKind.Sequential)] public struct Test { public byte…
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
3
votes
1 answer

How can I filter FieldInfos that are the underlying implementation of a class event?

I want to get all the fields of a class without getting the underlying implementations of the class event. type.GetFields(BindingFlags...) returns the nuderlying delegate for event fields. Does anyone knows how to filter them out ?
Izik Shmulewitz
  • 125
  • 1
  • 6
3
votes
1 answer

How to get the MethodInfo of a function of a class, without string comparison

Similar to a previous question of mine, when I was asking about getting the FieldInfo of a field, How to get the FieldInfo of a field from the value, from the answers there, I compiled this helper class, using System; using System.Reflection; using…
seaders
  • 3,878
  • 3
  • 40
  • 64
3
votes
2 answers

Getting the attributes of a field using reflection in C#

I wrote a method that extracts fields from an object like this: private static string GetHTMLStatic(ref Object objectX, ref List ExludeFields) { Type objectType = objectX.GetType(); FieldInfo[] fieldInfo = objectType.GetFields(); …
Mr. Smith
  • 5,489
  • 11
  • 44
  • 60
1
2 3 4