Reflection Newbie - More detailed problem description

  • Thread starter Thread starter Ron M. Newman
  • Start date Start date
R

Ron M. Newman

Hi,

what I really want is XML Persistence - the writing side is DONE, what I'm
working on is the reading. I am storing dynamic attributed objects. an
attributed object is a collection of named attributes and their values.

for example:

attribute name: "name"
attribute type: "System.String"
attribute value: "Mr. Smith"

attribute name "age"
attribute type: "Sytstem.Int32"
attribute value: "32"

my question is, once I have loaded the attribute description from XML and I
have the type and value, I'd like to re-create the object in memory and
assign th evalue to it. not everything supports Parse, so I might have to
decide that those objects that do not have "Parse" would use serialization
in writing and reading. Also, I know you can't just instantiate "String()"
it doesn't have an empty constructor prototype.

I guess what I need now is an advice on how you'd solve this problem. Is
there a golden rule I can use here? or am I doomed to make a private case
for Strings, things that have "Parse" and objects like images which must be
serialized.

Thanks
Ron
 
Have you looked at XMLSerializer? Why recreate the wheel?

If you must, it would go something like...

string obj = "Person";

string prop = "Age";

string typ = "System.Int32";

string val = "12";

object o = Activator.CreateInstance(Type.GetType(obj));

object b = val;

Type t = Type.GetType(typ);

PropertyInfo pi = o.GetType().GetProperty(prop, t);

if (pi != null)

{

if (t.IsAssignableFrom(typeof(int)))

b = int.Parse(val);

pi.SetValue(o, b, null);

}
 
Hi,

my data structures are very complex, lots of hashings and things that
XMLSerializer just cannot deal with automatically, plus, I don't need all
the information in the data structures and some of it is C# framework and I
can't just go there and change the XML directives ([XML......]) to match
what I need. So it seems like I have no choice but to "re-invent" the wheel
or flatten my world before writing, reading it flat, and un-flattening it
for re-use.

How do I create a String object as part of an automated process that has no
special cases when the only thing I have is "System.String" as a type? This
class doesn't have a default constructor.

Ron
 
Hi Ron,
How do I create a String object as part of an automated process that has no special cases when the only thing I have is
"System.String" as a type? This class doesn't have a default constructor.

If your pulling the value from an XML file then it's already a string. So if you encounter "System.String", then do nothing.

--
Dave Sexton

Ron M. Newman said:
Hi,

my data structures are very complex, lots of hashings and things that XMLSerializer just cannot deal with automatically, plus, I
don't need all the information in the data structures and some of it is C# framework and I can't just go there and change the XML
directives ([XML......]) to match what I need. So it seems like I have no choice but to "re-invent" the wheel or flatten my world
before writing, reading it flat, and un-flattening it for re-use.

How do I create a String object as part of an automated process that has no special cases when the only thing I have is
"System.String" as a type? This class doesn't have a default constructor.

Ron

Eric Cadwell said:
Have you looked at XMLSerializer? Why recreate the wheel?

If you must, it would go something like...

string obj = "Person";

string prop = "Age";

string typ = "System.Int32";

string val = "12";

object o = Activator.CreateInstance(Type.GetType(obj));

object b = val;

Type t = Type.GetType(typ);

PropertyInfo pi = o.GetType().GetProperty(prop, t);

if (pi != null)

{

if (t.IsAssignableFrom(typeof(int)))

b = int.Parse(val);

pi.SetValue(o, b, null);

}
 
Back
Top