Statically getting fields of classes without knowing the class definitions.

G

gz

Good morning everyone,

I am writing a module that needs to access fields in classes
efficiently. The trouble is that the module does not know the classes
beforehand, although the user of the module does know the classes in
compile time without resorting to reflection.

For example, I have a class A and I also have class Module. It knows
nothing about class A, but needs to access field f1. In C or C++, I
can tell class Module the offset and type of f1, and then it knows how
to access f1.

class A
{
public: int f1;
};

class Module
{
void Register(const char * class_type, const char *field_type, int
offset);

}

Module m;
m.Register("A", "int", (int)((char*)(&((A*)(0))->f1)));


How do I do this in C#?

Thanks,
GZ
 
M

Mattias Sjögren

How do I do this in C#?

Are you saying that using Reflection is not an option? Because if it
is, the easiest way is probably to pass a FieldInfo for the field in
question to the Register method.


Mattias
 
G

gz

Are you saying that using Reflection is not an option? Because if it
is, the easiest way is probably to pass a FieldInfo for the field in
question to the Register method.

Mattias

I know. Reflection is too slow because it is doing too much. All these
things are determined completely at compile time and therefore there
should be a way that does not require the overhead of dynamic methods
like reflection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top