Assign all members this.Something with reflection?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I need to do the following.
In the constructor of the class MyClass I have an object already oMyClass. I
want to assign programmatically all that is assignable of the "new" myClass
to the corresponding of oMyClass.
Say:
this.Something1=oMyClass.Something1
this.Something2 =oMyClassSomething2
....
I tried to play around with MemberInfo and alike, and articles/examples I
found describe rather to get information about members/properties etc.
However, there was no example how I can do both at the same time... loop
through this.Somethings simultaneously with oMyClass.Somethings and assign
the contents of one to the other.

Can someone refer me to a good example (or even post one)?

Thanks,
 
foreach (PropertyInfo property in aObj.GetType().GetProperties())
{
Object propertyValue = property.GetValue(aObj, null);
property.SetValue(bObj,propertyValue,null);

}

Regards,
Naveed Ahmad Bajwa
http://bajoo.blogspot.com/
 
Thanks for the reply,
however, it did not solve the question.
First I am not dealing only with Properties.
If I understand your example right, in my case it would be
foreach (PropertyInfo property in oMyClass.GetType().GetProperties())
{
Object propertyValue = property.GetValue(oMyClass, null);
property.SetValue(this,propertyValue,null);

}
So, what do I do with non-property types?
(The solution for my case is not to expose everything as properties, can't
do that unfortunately).

MemberInfo does not do it (at least not so obviously).


Thanks in advance,
 
Hi,
I a getting closer, I think...
I looked at "GetFields" also

so we have

foreach (FieldInfo field in oMyClass.GetType().GetFields())
{
Object fieldValue = field.GetValue(oMyClass);
field.SetValue(this, fieldValue);
}
and
foreach (PropertyInfo property in
oMyClass.GetType().GetProperties())
{
Object propertyValue = property.GetValue(oMyClass, null);
property.SetValue(this, propertyValue, null);

}


However, field.SetValue(this, fieldValue);
does ignore values for static fields, any solution for that?

And: have I caught "everything" that I can assign to?

Type.GetMembers get's me everything... however I see no obvious way to
access/assign the contents through MemberInfo in a similar way.

Andi
 
However, field.SetValue(this, fieldValue);
does ignore values for static fields, any solution for that?
Sorry, this was stupid of me...
in the example,
if I have a static field in the object (in the same object type), the
"value" already exists in the object since it is not an instance but static...

So - please ignore this part.
 
Hi

Based on the MSDN, we know that the memberinfo has limited property and
method.
It is a base class" of the PropertyInfo, we need to concrete class
PropertyInfo to do the concrete operation.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Peter,

thanks, but I did not ask that. I did not want to "concrete class"
MemberInfo. I had a specific thing in mind that I wanted to do - basically
set all "contents" of one object to the "contents" of another object of the
same type.
(In the specific example itwould usually be the "this" object since I cannot
assign anything to "this").

My last question was if I "caught everything" with what I found out so far.
MemberInfo was just one example I brought which did not help me.
So I iterated through the "GetFields" and "GetProperties" and "SetValue" in
both cases. Did I catch everything or is there anything I forgot?

Please tell me what you think you understood what I want to know.

Just because it was the last phrase in the post it does not mean that it was
the question.
 
Hi

There are a lot of information in the Reflection. You may check with the
System.Reflection Namespace in MSDN, it is hard to enumerate them all here.
Such as EventInfo, ConstructorInfo, ParameterInfo and MethodInfo and etc.
Also we can get the Assembly or Module information via Reflection.

Basically, the Filed and Property are the mainly storage metadata.
This.Field = "dfd"
This.Property = "dfdf"
Commonly a property is a method to read/write a field.

Also this.MethodCall is method call which is not storage. But we can
enumerate how many methods defined in certian Type.

For detailed information I suggest try take a look at the book below, which
explained detailed about metadata.

Essential .NET, Volume I: The Common Language Runtime (Paperback)
http://www.amazon.com/gp/product/0201734117/002-0578527-8545655?v=glance&n=2
83155


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Peter,

I think we have a communication problem.

I did not ask what is available in "Reflection".

I had a very specific scenario...
I will rephrase it...
I want to transfer as much information/state as possible from one object
instance to another object instance of the same type when I cannot say
"object1=object2;".

In my case one object is the newly created object ("this").
I am well aware that I cannot "assign" a method definition from one object
to the other object.
I will outline what I need to do...
in the (non-default) constructor of the object, I am (through a
deserializer) getting an instance of the object. (Not doing it in the
constructor is not an option)
The new object "this" should now take what the deserialized object has.

Since I cannot do
this=myNewObject,
before, for every new class, I would need to write code like
this.memberObject1= myNewObject.memberObject1;
this.memberObject2= myNewObject.memberObject2;
....
same for properties... and so on.

So I tried to come up with a generic solution that transfers all state from
myNewObject to the "this"-new object.

So, with the following, did I catch everything? - or you do not know?

(I _know_ I do not need to go through e.g. the methods with reflection.)

The solution so far:

calling:
SetObjectContents(myNewObject,this);



public static void SetObjectContents(Object oSourceObject, Object
oTargetObject)
{
SetPublicObjectProperties(oSourceObject, oTargetObject);
SetPublicObjectFields(oSourceObject, oTargetObject);
}


public static void SetPublicObjectProperties(Object oSourceObject,
Object oTargetObject)
{
Type tSourceObjectType;
tSourceObjectType = oSourceObject.GetType();
Object oValue;
foreach (PropertyInfo oPropertyInfo in
tSourceObjectType.GetProperties())
{
if (oPropertyInfo.PropertyType.IsPublic)
{
oValue = oPropertyInfo.GetValue(oSourceObject, null);
oPropertyInfo.SetValue(oTargetObject, oValue, null);
}
}
}

public static void SetPublicObjectFields(Object oSourceObject,
Object oTargetObject)
{
Type tSourceObjectType;
tSourceObjectType = oSourceObject.GetType();


Object oValue;
foreach (FieldInfo oFieldInfo in tSourceObjectType.GetFields())
{
if (oFieldInfo.IsPublic)
{
oValue = oFieldInfo.GetValue(oSourceObject);
oFieldInfo.SetValue(oTargetObject, oValue);

}
}
}

Thanks in advance,
 
Hi

From you description, what you want is a clone behavior.

ICloneable Interface
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemicloneableclasstopic.asp

Implementing ICloneable
http://blogs.msdn.com/brada/archive/2003/04/09/49935.aspx

But in what scenario, did you want the behavior?
If remoting scenario, that will pass a object from one appdomain to
another, we can inherit a class from MarshalByRefObject or implement the
ISerializable.

ISerializable Interface
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemruntimeserializationiserializableclasstopic.asp

Remotable Objects
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconRemotableObjects.asp

Hope this helps.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top