Get object name
-
Using .NET CF does anyone know if its possible to get an objects instance name. ie the variable name. I want to serialise a bunch of objects and it would be nice for each to include its nme with data to allow better versioning eg
class MyObject { ..... public Write ( TextWriter tw) { tw.Write( this.GetType().ToString() ); // this is ok tw.Write( this.GetMyInstanceNameIfPossible ); // well ,you know what I mean }
So I can later doMyObject.Write( writer );
This way when I read it in I can decide what variable to init. rather that rely on the order the data was written out. I know I can just manually write the name, but this would be automatic and less prone to error. Thanks .nuetter -
Using .NET CF does anyone know if its possible to get an objects instance name. ie the variable name. I want to serialise a bunch of objects and it would be nice for each to include its nme with data to allow better versioning eg
class MyObject { ..... public Write ( TextWriter tw) { tw.Write( this.GetType().ToString() ); // this is ok tw.Write( this.GetMyInstanceNameIfPossible ); // well ,you know what I mean }
So I can later doMyObject.Write( writer );
This way when I read it in I can decide what variable to init. rather that rely on the order the data was written out. I know I can just manually write the name, but this would be automatic and less prone to error. Thanks .nuetterAre you asking how to get an instance variable's name? For instance, if I wrote:
public void DoSomething() { string myFoo = new FooType(); }
would you want to know, at serialization time, that the variable name was 'myFoo
'? If so, what advantage does that provide? In any case, I don't think that's possible at runtime. A quick look at the ILDASM (Intermediate Language) representation of the class above makes it clear that locally scoped member variable names are discarded in the compilation to IL. It is probably important to note that the names of field-level variables, method names etc., whether public or private, are preserved during compilation, so technically you could use reflection to get the names. However, if your class(es) implement theISerializable
interface (as opposed, or in addition to being marked with[SerializableAttribute]
), then you are free to develop your own serialization and deserialization plans for the class(es). TheISerializable
interface exists to deal with just this kind of issue (provide developers with a way to prevent deserialization exceptions caused by versioning), among other things. Hope this helps.The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
-
Using .NET CF does anyone know if its possible to get an objects instance name. ie the variable name. I want to serialise a bunch of objects and it would be nice for each to include its nme with data to allow better versioning eg
class MyObject { ..... public Write ( TextWriter tw) { tw.Write( this.GetType().ToString() ); // this is ok tw.Write( this.GetMyInstanceNameIfPossible ); // well ,you know what I mean }
So I can later doMyObject.Write( writer );
This way when I read it in I can decide what variable to init. rather that rely on the order the data was written out. I know I can just manually write the name, but this would be automatic and less prone to error. Thanks .nuetterYou would have to manually parse the debugging information; which, of course, does not exist in release builds. The best you could hope for is the address (using the &[^] Operator in an unsafe[^] context). PeterRitchie.com