help translating vb code to c#
i have this vb.net code, which may not even work in vb.net itself, but it's an example someone posted. It is suppose to copy all the properties of an object without having to code each individual property.
Code:
For Each [property] As PropertyInfo In o2.GetType().GetProperties()
[property].SetValue(o1, [property].GetValue(o2, Nothing), Nothing)
Next [property]
Re: help translating vb code to c#
You can use the VB.NET > C# converter page found here -
http://www.developerfusion.co.uk/uti...btocsharp.aspx
Re: help translating vb code to c#
If you want to copy an object, just use binary serialization.
You don't need to think about propeties at all.
Re: help translating vb code to c#
Quote:
Originally Posted by benmartin101
i have this vb.net code, which may not even work in vb.net itself, but it's an example someone posted. It is suppose to copy all the properties of an object without having to code each individual property.
Code:
For Each [property] As PropertyInfo In o2.GetType().GetProperties()
[property].SetValue(o1, [property].GetValue(o2, Nothing), Nothing)
Next [property]
This shouldn't be difficult to convert at all. It's just syntax.
Also keep in mind that you don't need the icky brackets around "property" to differentiate it from "Property" in C#, since C# is case sensitive.
Code:
foreach(PropertyInfo property in o2.GetType().GetProperties())
{
property.SetValue(o1, property.GetValue(o2, null), null);
}
Re: help translating vb code to c#
thanks for the responses.
I'll try your suggestion sunburnt.
Wossname, I tried using serialization, but you see, I'm using a COM object. The class that i'm using is in a COM object and when I try to binary serialize, it won't let me. I think it's because I have to declare the class as being <serializable>. Unfortunately, I don't know how to make classes from a COM object to be serializable.