M
Markus
Hello all,
I am working on coding a custom Formatter to be used to
export and restore property settings from objects on a
form in an easily edited text format. It's not true
serialization and deserialization because I'm not fully
describing objects or instantiating them during
deserialization (just storing and later setting properties
which meet certain Attributes).
The serialization works and now I am working on the
deserializing portion and am trying to figure out how to
obtain a type from a type string so I can read the data
back in.
The GetType() method only works for types which are
included in my assembly or if I explicitly obtain the
assembly - otherwise it returns null.
For example, my file would contain a line like:
"Size System.Drawing.Size {Width=40, Height=25}"
which would tell the code that this object's
property "Size" of type "System.Drawing.Size" has the
value "{Width...}"
I need to obtain a type object from "System.Drawing.Size"
to properly parse the data of this property.
So, is there any way to obtain the assembly of an object
derived from system based solely on its string? I don't
want a hard-coded solution because it would be preferable
if the code would work with future releases of the .NET
redistributable and also I don't want to look up the
correct dll for each property to make the method more
robust.
Here is a brief fragment which is as close as I got
(gleaned off another website):
string assemblyloc =
Environment.GetEnvironmentVariable("systemroot") +
"\\Microsoft.Net\\Framework\\v" +
System.Environment.Version.ToString()+
"\\System.Windows.Forms.dll";
assembly.LoadFrom(assemblyloc);
This code still has 2 problems: the dll file is hardcoded,
and the version string includes an additional field which
causes it to generate the incorrect path (my path
is: ...\Microsoft.NET\Framework\v1.0.3705\", but the value
returned by System.Environment.Version is 1.0.3705.xxxx -
this xxxx field would need to be removed to generate the
correct path).
Many thanks for any advice,
Markus
PS - as I type all this in, I came up with a possible
solution - which isn't a solution to the problem above,
but might work for me...
I will probably try to do things from the other end -
instead of getting type information from the system which
requires me to get its assembly, I could instead get it
from my own objects, since their type information is in my
assembly.
For example, my serialization Formatter might produce the
following:
"MyNamespace.MyClass 1 #"
"Size System.Drawing.Size {Width=1, Height=1}"
Where the first line describes the object, 1 indicates the
number of property entries for this object, and #
represents the object's ID property (used for persistence).
I can use reflection on "MyNamespace.MyObject" to get a
reference to this object's properties, then use "Size" to
obtain a reference to the various parts of its Size
property.
Still, I would be very interested if anyone has a solution
to the problem above. Thanks.
I am working on coding a custom Formatter to be used to
export and restore property settings from objects on a
form in an easily edited text format. It's not true
serialization and deserialization because I'm not fully
describing objects or instantiating them during
deserialization (just storing and later setting properties
which meet certain Attributes).
The serialization works and now I am working on the
deserializing portion and am trying to figure out how to
obtain a type from a type string so I can read the data
back in.
The GetType() method only works for types which are
included in my assembly or if I explicitly obtain the
assembly - otherwise it returns null.
For example, my file would contain a line like:
"Size System.Drawing.Size {Width=40, Height=25}"
which would tell the code that this object's
property "Size" of type "System.Drawing.Size" has the
value "{Width...}"
I need to obtain a type object from "System.Drawing.Size"
to properly parse the data of this property.
So, is there any way to obtain the assembly of an object
derived from system based solely on its string? I don't
want a hard-coded solution because it would be preferable
if the code would work with future releases of the .NET
redistributable and also I don't want to look up the
correct dll for each property to make the method more
robust.
Here is a brief fragment which is as close as I got
(gleaned off another website):
string assemblyloc =
Environment.GetEnvironmentVariable("systemroot") +
"\\Microsoft.Net\\Framework\\v" +
System.Environment.Version.ToString()+
"\\System.Windows.Forms.dll";
assembly.LoadFrom(assemblyloc);
This code still has 2 problems: the dll file is hardcoded,
and the version string includes an additional field which
causes it to generate the incorrect path (my path
is: ...\Microsoft.NET\Framework\v1.0.3705\", but the value
returned by System.Environment.Version is 1.0.3705.xxxx -
this xxxx field would need to be removed to generate the
correct path).
Many thanks for any advice,
Markus
PS - as I type all this in, I came up with a possible
solution - which isn't a solution to the problem above,
but might work for me...
I will probably try to do things from the other end -
instead of getting type information from the system which
requires me to get its assembly, I could instead get it
from my own objects, since their type information is in my
assembly.
For example, my serialization Formatter might produce the
following:
"MyNamespace.MyClass 1 #"
"Size System.Drawing.Size {Width=1, Height=1}"
Where the first line describes the object, 1 indicates the
number of property entries for this object, and #
represents the object's ID property (used for persistence).
I can use reflection on "MyNamespace.MyObject" to get a
reference to this object's properties, then use "Size" to
obtain a reference to the various parts of its Size
property.
Still, I would be very interested if anyone has a solution
to the problem above. Thanks.