Click here to Skip to main content
15,902,189 members
Home / Discussions / C#
   

C#

 
Generalloop throu dataset Pin
Totoo15-Sep-04 7:47
Totoo15-Sep-04 7:47 
GeneralInstalling Windows Services Pin
Guillermo Jimenez15-Sep-04 7:36
Guillermo Jimenez15-Sep-04 7:36 
GeneralRe: Installing Windows Services Pin
Heath Stewart15-Sep-04 7:47
protectorHeath Stewart15-Sep-04 7:47 
GeneralRe: Installing Windows Services Pin
Guillermo Jimenez15-Sep-04 10:04
Guillermo Jimenez15-Sep-04 10:04 
GeneralRe: Installing Windows Services Pin
Heath Stewart15-Sep-04 10:16
protectorHeath Stewart15-Sep-04 10:16 
GeneralRe: Installing Windows Services Pin
Guillermo Jimenez16-Sep-04 4:27
Guillermo Jimenez16-Sep-04 4:27 
GeneralRe: Installing Windows Services Pin
Heath Stewart16-Sep-04 5:31
protectorHeath Stewart16-Sep-04 5:31 
GeneralLooking for a generic method for accessing fields in C# classes/structs Pin
johnbMA15-Sep-04 7:06
johnbMA15-Sep-04 7:06 
Generic method for accessing fields in C# classes/structs
==========================================================
Previously in the unmanaged C++ world, a client could declare a structure of any length, cast it to a VOID pointer to memory, and then just deal with it as a memory location. This is sort of a rough and tumble version of polymorphism.

Consider the following example in unmanaged C++:

Client code:
===========
typedef struct dimStruct
{
double x;
double y;
int location;
};

SumFunc()
{
int aFields[] = { X_DIM, Y_DIM, LOCATION };
int iNumberItems = 3;
dimStruct dims;

services->FillTheStruct( (PVOID)&dims, aFields, iNumberItems );

}

Services Code
=============
Services::FillTheStruct( PVOID pData, int aFields[], int iNumberItems )
{
//** Now loop through the field array and stuff values into the
//** correct offsets in memory.

int itemOffset = 0;

for ( int I =0; I < iNumberItems; i++ )
{
int sizeOfItem = GoGetDataItem( &pData[itemOffset], aFields[i] );

//** increment the offset within the pData memory block
itemOffset += sizeOfItem;
}

}


I was able to replicate the functionality in C# (close. but not quite) through the following:

Client Code
===========
object[] aObjects = new object[3];

aObjects[1] = dimStruct.x; //** double
aObjects[2] = dimStruct.y; //** double
aObjects[3] = dimStruct.location; //** int

services->FillTheStruct( aObjects, aFields );

dimStruct.x = aObjects[1]; //** double
dimStruct.y = aObjects[2] //** double
dimStruct.location = aObjects[3] //** int


ServicesCode
============
FillTheStruct( object[] aObjects, int[] aFields )
{

//** Get the array of types
Type aType[] aTypes = type.GetTypeArray( aObjects );

int iSize = 0;

//** Figure out the size (to simplfy examples, ignore strings)
foreach ( Type oneType in aType )
{
iSize += marshal.SizeOf( oneType );
}

byte[] aBytes = new byte[ iSize ];

//** Magically stuff data into byte array
Services->FillTheStruct( aBytes, aFields );

//** Now, fill the client’s aObject
MemoryStream stream = new MemoryStream( aBytes );
BinaryReader reader = new BinaryReader( stream );

int iFieldCount = aObjects.GetLength( 0 );

for ( int index =0; index < iFieldCount; index++ )
{
Type oneType = aTypes[ index ];

If ( oneType.Equals( typeof( double )))
{
aObjects[ index ] = reader.ReadDouble();
}

else if ( oneType.Equals( typeof( int )))
{
aObjects[ index ] = (int)reader.ReadInt32()
}

//** And so on….
}

}

So the questions are:
====================
Is it possible to replicate the original solution in C# without using an object[] array as an intermediary?

Is there a generic way to walk through an unknown class/struct, get the fields, their types, and then make assignments into that structure/class?

I would have to have a method that accepts some sort of generic object to start.

Thanks in advance,
JohnB
GeneralRe: Looking for a generic method for accessing fields in C# classes/structs Pin
Nnamdi Onyeyiri15-Sep-04 7:31
Nnamdi Onyeyiri15-Sep-04 7:31 
GeneralRe: Looking for a generic method for accessing fields in C# classes/structs Pin
Heath Stewart15-Sep-04 7:42
protectorHeath Stewart15-Sep-04 7:42 
GeneralRe: Looking for a generic method for accessing fields in C# classes/structs Pin
Nick Parker15-Sep-04 7:46
protectorNick Parker15-Sep-04 7:46 
QuestionMax length of text in statusbar? Pin
blakeb_115-Sep-04 6:12
blakeb_115-Sep-04 6:12 
AnswerNevermind, found the answer Pin
blakeb_115-Sep-04 6:14
blakeb_115-Sep-04 6:14 
GeneralRe: Nevermind, found the answer Pin
Heath Stewart15-Sep-04 6:59
protectorHeath Stewart15-Sep-04 6:59 
GeneralRe: Nevermind, found the answer Pin
blakeb_117-Sep-04 5:34
blakeb_117-Sep-04 5:34 
GeneralScene change detection doubt Pin
ee9903515-Sep-04 5:55
ee9903515-Sep-04 5:55 
GeneralRe: Scene change detection doubt Pin
Heath Stewart15-Sep-04 6:54
protectorHeath Stewart15-Sep-04 6:54 
GeneralRe: Scene change detection doubt Pin
ee9903515-Sep-04 8:04
ee9903515-Sep-04 8:04 
GeneralRe: Scene change detection doubt Pin
Manajit19-May-10 20:04
Manajit19-May-10 20:04 
GeneralLPWSTR in C# Pin
yyf15-Sep-04 5:41
yyf15-Sep-04 5:41 
GeneralRe: LPWSTR in C# Pin
Heath Stewart15-Sep-04 7:10
protectorHeath Stewart15-Sep-04 7:10 
GeneralRe: LPWSTR in C# Pin
yyf15-Sep-04 9:43
yyf15-Sep-04 9:43 
GeneralRe: LPWSTR in C# Pin
Heath Stewart15-Sep-04 10:16
protectorHeath Stewart15-Sep-04 10:16 
GeneralRe: How to change the text of a text object at runtime in crystal reports for .NET (C#) Pin
Dave Kreskowiak15-Sep-04 3:56
mveDave Kreskowiak15-Sep-04 3:56 
GeneralRe: wall on this one:simple string search question Pin
sreejith ss nair15-Sep-04 3:46
sreejith ss nair15-Sep-04 3:46 

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.