Relection question

  • Thread starter Thread starter fj
  • Start date Start date
F

fj

I am wondering if anyone has this same problem and what's the solution.

I am assigning values to an object, for example,
ObjEntityPerson.Edit[0].Item.KeyValue = "12345";

However, the Edit[] might be null, Edit[0] and Item might be null.
Eventually I end up with check each object, if null then initialize it, then
assign it a value.

I want use reflection to write a function like

public Object InitIfNull(byRef Object obj, Type MyType)

{

//Check if obj if null

if( obj == null)

{

ConstructorInfo ctor = typeof(MyType).GetConstructor();

InstanceDescriptor desc = new InstanceDescriptor(ctor, new object [] {});

//bad code, I have to do my research, but anyways, you get an idea. Get the
constructor and create the object.


}

}

My question is is it possible to give any object like
ObjEntityPerson.Edit[0].Item.KeyValue, can I create the object in a
hierarchy?

Thanks

-fj
 
This code is what I use to dynamically create objects at run-time based
on an arbitrary type:

if (obj==null){
obj = myType.GetConstructors()[0].Invoke(null); // I am making a big
assumption that the default constructor.
}
 
This code is what I use to dynamically create objects at run-time based
on an arbitrary type:

if (obj==null){
obj = myType.GetConstructors()[0].Invoke(null); // I am making a big
assumption that the default constructor.

}

--
Bryan Phillips
MCT, MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
Web Site: http://www.composablesystems.net


I am wondering if anyone has this same problem and what's the solution.
I am assigning values to an object, for example,
ObjEntityPerson.Edit[0].Item.KeyValue = "12345";
However, the Edit[] might be null, Edit[0] and Item might be null.
Eventually I end up with check each object, if null then initialize it, then
assign it a value.
I want use reflection to write a function like
public Object InitIfNull(byRef Object obj, Type MyType)

//Check if obj if null
if( obj == null)

ConstructorInfo ctor = typeof(MyType).GetConstructor();
InstanceDescriptor desc = new InstanceDescriptor(ctor, new object [] {});
//bad code, I have to do my research, but anyways, you get an idea. Get the
constructor and create the object.

My question is is it possible to give any object like
ObjEntityPerson.Edit[0].Item.KeyValue, can I create the object in a
hierarchy?

-fj

Hi,

Further more,

You can use Assembly.CreateInstance()


Moty
 
Back
Top