Click here to Skip to main content
15,913,919 members
Home / Discussions / C#
   

C#

 
Question[SOLVED] How do I get the type of the class I'm in without knowing its name? [modified] Pin
blackblizzard18-Feb-10 23:44
blackblizzard18-Feb-10 23:44 
AnswerRe: How do I get the type of the class I'm in without knowing its name? Pin
Abhinav S19-Feb-10 0:47
Abhinav S19-Feb-10 0:47 
GeneralRe: How do I get the type of the class I'm in without knowing its name? Pin
blackblizzard19-Feb-10 1:03
blackblizzard19-Feb-10 1:03 
GeneralRe: How do I get the type of the class I'm in without knowing its name? Pin
Daniel Grunwald19-Feb-10 2:22
Daniel Grunwald19-Feb-10 2:22 
GeneralRe: How do I get the type of the class I'm in without knowing its name? Pin
blackblizzard19-Feb-10 2:35
blackblizzard19-Feb-10 2:35 
QuestionSql Connection Pin
Isaac Gordon18-Feb-10 23:29
Isaac Gordon18-Feb-10 23:29 
AnswerRe: Sql Connection Pin
Eddy Vluggen19-Feb-10 1:05
professionalEddy Vluggen19-Feb-10 1:05 
QuestionGet an "index" for a field of a class? Pin
blackblizzard18-Feb-10 23:29
blackblizzard18-Feb-10 23:29 
Hi everyone.
I'm having trouble with the following: I need a way to distinguish the different fields of a class by means of integers -- for example, given the following class:

class MyClass {<br />
<br />
float floatField;<br />
double[] arrayField;<br />
Object objectField;<br />
<br />
}



I'd like to be able to identify floatField as field 0, arrayField as field 1, and objectField as field 2. I don't really care about the order, but I need the integers to be consecutive and to never change throughout execution. They can change between executions.

I haven't seen any straightforward method for this in Type. The best I can come up with is to call typeof(MyClass).GetFields() and use the order the fields in the return array as their id. For example, if it returns [floatField, arrayField, objectField], it would return in the ids I mentioned above (0, 1 and 2, respectively).

I'm going to need to find the id of a given field pretty often throughout execution, so I create a dictionary that maps the name of a field to its index, like so:

int i = 0;
Dictionary<string, int> fieldOffsetDict = new Dictionary<string, int>();
foreach (FieldInfo field in typeof(MyClass).GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)) {
    fieldOffsetDict[field.Name] = i++;
}


Anyone can think of a better way of doing this (for example, is there a method that I've failed to see that would do this directly), or do I keep it as it is? I can't help but feel this is a bit of an overkill, so I'd like to make sure.

Also, if I didn't mind the ids not being consecutive, would there be a better way to do this?

Thanks.
AnswerRe: Get an "index" for a field of a class? Pin
Luc Pattyn19-Feb-10 0:21
sitebuilderLuc Pattyn19-Feb-10 0:21 
GeneralRe: Get an "index" for a field of a class? Pin
blackblizzard19-Feb-10 0:30
blackblizzard19-Feb-10 0:30 
QuestionMemory Leak in WPF. Multiple instances of the same control in memory. [modified] Pin
AWriter18-Feb-10 23:03
AWriter18-Feb-10 23:03 
AnswerRe: Memory Leak in WPF. Multiple instances of the same control in memory. Pin
Saksida Bojan19-Feb-10 20:21
Saksida Bojan19-Feb-10 20:21 
Questiongrid view row copy Pin
Syed Shahid Hussain18-Feb-10 23:01
Syed Shahid Hussain18-Feb-10 23:01 
AnswerRe: grid view row copy Pin
Saksida Bojan19-Feb-10 20:14
Saksida Bojan19-Feb-10 20:14 
QuestionDatagridViewColumn displayIndex - column order wrong after setting them from control settings table. Pin
PoppysDad18-Feb-10 22:48
PoppysDad18-Feb-10 22:48 
AnswerRe: DatagridViewColumn displayIndex - column order wrong after setting them from control settings table. Pin
Vishal Bhatt115-Sep-10 22:58
Vishal Bhatt115-Sep-10 22:58 
AnswerRe: DatagridViewColumn displayIndex - column order wrong after setting them from control settings table. Pin
Zombie_Inc18-Nov-10 20:24
Zombie_Inc18-Nov-10 20:24 
GeneralRe: DatagridViewColumn displayIndex - column order wrong after setting them from control settings table. Pin
henhao8-May-11 7:23
henhao8-May-11 7:23 
QuestionHow to check if Excel-workbook is empty? Pin
Thomas ST18-Feb-10 22:28
Thomas ST18-Feb-10 22:28 
AnswerRe: How to check if Excel-workbook is empty? Pin
Thomas ST18-Feb-10 22:41
Thomas ST18-Feb-10 22:41 
GeneralRe: How to check if Excel-workbook is empty? Pin
Member 766206128-Feb-11 21:40
Member 766206128-Feb-11 21:40 
QuestionTemporary Modification in a Control During Drag and Drop Pin
Som Shekhar18-Feb-10 22:11
Som Shekhar18-Feb-10 22:11 
AnswerRe: Temporary Modification in a Control During Drag and Drop Pin
Luc Pattyn19-Feb-10 0:26
sitebuilderLuc Pattyn19-Feb-10 0:26 
GeneralRe: Temporary Modification in a Control During Drag and Drop Pin
Som Shekhar19-Feb-10 0:35
Som Shekhar19-Feb-10 0:35 
Questionproject using c#.net and sql server 2005 Pin
savitha87k18-Feb-10 20:37
savitha87k18-Feb-10 20:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.