473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Statically getting fields of classes without knowing the class definitions.

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

Nov 13 '07 #1
2 1112
>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.
Nov 13 '07 #2
gz
On Nov 13, 4:29 pm, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
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/dotnet/|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.
Nov 17 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
1
by: Stephan | last post by:
Hi, I'm using Visual Studio 2003 (C#) with the integrated Crystal Report software and have the following question: How can I assign a value (string) to an unbound (string) field in Crystal...
12
by: williamc | last post by:
Is there anything wrong with having several classes with the same name in the same style sheet? Something like... div.pagedown { margin: 20px 0px 20px 0px; border-top: 1px solid #caa;...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
6
by: Jon Slaughter | last post by:
Is there any way to get the class type from a pointer? I'm working on a class that acts as a set: I have two classes, one called FSet and the other called Element. FSet inherets Element and...
12
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc..,...
4
by: glenn | last post by:
Is there a way to loop through a classes fields without first knowing what fields are there? Trying to write a generic class that could build a query based on a classes fields and types. This...
4
by: puzzlecracker | last post by:
I have seen these terms used in Gang of 4 but could never thoroughly understand what it meant I perceive that C++ is both, but apparently it is only a latter. Can someone explain it? thx
6
by: s99999999s2003 | last post by:
hi i come from a non OO environment. now i am learning about classes. can i ask, in JAva, there are things like interface. eg public interface someinterface { public somemethod (); .... ... }...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.