Assigning properties Values from one object to other

P

parez

Hi


I want to assign the values of properties of object A to objectB

They both belong to different classes but implement on common
interface. I just want to assign the properties defined in the
interface.

Will this work?

ClassA a = new ClassA()
classb b = new classB();

IMyinterface c= a;
b=c;


or should i write a function

BindData(object source, object target, Type interfaceType)

and use refelection
 
M

Marc Gravell

I guess... although reflection should generally be a last resort.
Would it be too hard to write a handful of methods to move a few
interrfaces?

You might want to think about the values that exist - i.e. if any are
collections, what should happen? or is it just standard settable
values?

I would probably be tempted to use generics to ensure that source and
target are suitable at compile time.

Example below (also covers readonly / writeonly).

Marc

using System.ComponentModel;

class Foo : IBar
{
public int Prop1 { get; set; }
public string Name { get; set; }
string IBar.Prop2 { get { return Name; } set { Name = value; } }
public bool Prop3 { get { return true; } }
public bool Prop4 { set { } }
}
interface IBar
{
int Prop1 { get; set; }
string Prop2 { get; set; }
bool Prop3 { get; }
bool Prop4 { set; }
}
class Program
{
static void Main()
{
Foo s = new Foo(), t = new Foo();
s.Name = "Fred";
s.Prop1 = 123;
BindData(s, t);
}
static void BindData<T>(T source, T target)
{
foreach (PropertyDescriptor prop in
TypeDescriptor.GetProperties(typeof(T)))
{
if (!prop.IsReadOnly) prop.SetValue(target,
prop.GetValue(source));
}
}

}
 
M

Marc Gravell

Oops - example should have used explicit generic call:

BindData<IBar>(s, t);

Otherwise the compiler infers T = Bar, and other properties (not part
of the interface) get copied. But just call it with the interface and
it will work - but it checks (at compile time) that both "source" and
"target" are suitable for T.

Marc
 
P

parez

Oops - example should have used explicit generic call:

BindData<IBar>(s, t);

Otherwise the compiler infers T = Bar, and other properties (not part
of the interface) get copied. But just call it with the interface and
it will work - but it checks (at compile time) that both "source" and
"target" are suitable for T.

Marc

Generic is definitely better than my way. i was going to add runtype
check but i guess i dont needed it now..

Thanks..yea it displayed all the properties. and the new thing display
only the interface properties.
 
P

parez

Oops - example should have used explicit generic call:

BindData<IBar>(s, t);

Otherwise the compiler infers T = Bar, and other properties (not part
of the interface) get copied. But just call it with the interface and
it will work - but it checks (at compile time) that both "source" and
"target" are suitable for T.

Marc

Is there any way i can force the user to pass IBar / or any other
interface for everycall?
 
M

Marc Gravell

Do you mean a *specific* interface, or just "an interface" ?

For the first, just use specific overloads on the public API:

BindData(IFoo source, IFoo target) {...}
BindData(IBar source, IBar target) {...}

Either doing things manually, or calling private BindData<T>
internally.

If you mean "an interface" - there is no generic constraint for this.
You could perhaps just add (to BindData<T>):
if (!typeof(T).IsInterface) throw new
InvalidOperationException(typeof(T).Name + " is not an interface");

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top