Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an object instance from FieldInfo

Tags:

c#

reflection

So my problem is this: I have an object instance represented only by FieldInfo. I have no other access to that object. Is it possible to get that object instance only through the field info like so?

MyObject myObject = fieldInfo.SomeMethodOrProperty as MyObject; 

This is just only an illustration, but I think you get the picture.

like image 320
Mama Tate Avatar asked May 01 '26 13:05

Mama Tate


1 Answers

No, the FieldInfo represents attributes and metadata of a field in regards to a type. It is not attached to a specific instance. When you want to use it, you have to pass in the instance you want it to use (through GetValue or SetValue)

like image 81
samy Avatar answered May 03 '26 04:05

samy