Statically getting fields of classes without knowing the class definitions.
am 13.11.2007 18:57:08 von 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
Re: Statically getting fields of classes without knowing the class definitions.
am 13.11.2007 23:29:22 von mattias.dont.want.spam
>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
--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Re: Statically getting fields of classes without knowing the class
am 17.11.2007 22:13:37 von Gz
On Nov 13, 4:29 pm, Mattias Sjögren
wrote:
> >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
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotn=
et/|http://www.dotnetinterop.com
> Please reply only to the newsgroup.
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.