How would you improve this shallow copying class? - c#

I've written a class with a single static method that copies property values from one object to another. It doesn't care what type each object is, only that they have identical properties. It does what I need, so I'm not engineering it further, but what improvements would you make?
Here's the code:
public class ShallowCopy
{
public static void Copy<From, To>(From from, To to)
where To : class
where From : class
{
Type toType = to.GetType();
foreach (var propertyInfo in from.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
{
toType.GetProperty(propertyInfo.Name).SetValue(to, propertyInfo.GetValue(from, null), null);
}
}
}
I'm using it as follows:
EmployeeDTO dto = GetEmployeeDTO();
Employee employee = new Employee();
ShallowCopy.Copy(dto, employee);

Are your DTOs serializable? I would expect so, in which case:
MemberInfo[] sm = FormatterServices.GetSerializableMembers(typeof(From));
object[] data = FormatterServices.GetObjectData(from, sm);
FormatterServices.PopulateObjectMembers(to, sm, data);
But note that I don't really agree with this general approach. I would prefer a strong contract for copying on your DTOs that each DTO implements.

Change your type parameter names to comply with naming conventions, e.g. TFrom and TTo, or TSource and TDest (or TDestination).
Do most of your work in a generic type instead of in just a generic method. That allows you to cache the properties, as well as allowing type inference. Type inference is important on the "TFrom" parameter, as it will allow anonymous types to be used.
You could potentially make it blindingly fast by dynamically generating code to do the property copying and keeping it in a delegate which is valid for the "from" type. Or potentially generate it for every from/to pair, which would mean the actual copying wouldn't need to use reflection at all! (Preparing the code would be a one-time hit per pair of types, but hopefully you wouldn't have too many pairs.)

A new method that created a new instance of To and called the Copy() method before returning might be useful.
Like this:
public static To Create<From, To>(From from)
where To : class, new()
where From : class
{
var to = new To();
Copy(from, to);
return to;
}

Decide what you want to do if passed objects of types that share some properties but not all. Check for the existence of the property in the From object in the To object before trying to set it's value. Do the "right thing" when you come to a property that doesn't exist. If all of the public properties need to be identical, then you will need to check if you've set all of them on the To object and handle the case where you haven't appropriately.
I'd also suggest that you may want to use attributes to decorate the properties that need to be copied and ignore others. This would allow you to go back and forth between the two different objects more easily and continue to maintain some public properties that are derived rather than stored on your business object.

Related

Construct an instance of a concrete type of a generic, using reflection at runtime

Consider the following class:
public class GenericClass<T>
{
public T Argument;
}
And a second class that references and uses the GenericClass:
public class ClientClass
{
GenericClass<T> genclass = new GenericClass<T>();
}
My problem is that I don't know the type of T at compile time. I am using reflection during runtime to obtain the type of T. Is it possible somehow to use a parametrized expression while instantiating the GenericClass?
Yes, but you'll have to use reflection to build the actual type for which you're looking.
You first need to get the open generic, by using the typeof operator:
var openType = typeof(GenericClass<>);
Next, you need to build the specific generic you want. Say your desired type T is stored in a variable, type:
var closedType = openType.MakeGenericType(type);
Finally, use reflection to create an instance of that type.
object instance = Activator.CreateInstance(closedType);
As noted by xanatos in the comments, however, you should be aware that this results in a member of type object. To be able to manipulate the object without reflection, you have two choices.
You can create a parent class, GenericClass, from which GenericClass<T> derives, and include methods on it that are common to all. (i.e. GenericClass contains members that don't need to use T.
As Ingenu had mentioned in the comments on the question, you can create an interface IInterface, and then add a restriction on T, where T : IInterface. Then, you can cast instance to GenericClass<IInterface> and manipulate it that way. Obviously, type must implement IInterface in this case.
You can of course also just keep the object reference and manipulate it using reflection only, or use dynamic to late-bind any method calls -- this uses reflection under the hood, however.

Setting Values on a derived class from the base class constructor using Reflection

I have two classes like this:
public abstract class MyBase
{
protected MyBase(){
Initialize();
}
protected IDictionary<string,string> _data;
private void Initialize() {
// Use Reflection to get all properties
// of the derived class (e.g., call new MyDerived() then
// I want to know the names "Hello" and "ID" here
var data = GetDataFromBackend(propertyNamesFromDerived);
_data = data;
}
}
public class MyConcrete : MyBase
{
public MyConcrete(){
// Possibly use Reflection here
Hello = _data["Hello"];
ID = new Guid(data["ID"]);
}
public string Hello {get;set;}
public Guid ID {get; set;}
}
As you see, I want the constructor of my base class to know about the properties of the derived class I'm instantiating.
Now, this seems like a huge and big code smell, so let me give some more background about my intentions, maybe there is a better way.
I have a backend system that stores Key/Value Pairs, essentially a Dictionary<string,string>. I want to abstract away working with this backend system in a way where people can create classes whose properties are Keys into the backend system. When they construct this object, it will automatically load the data from that system and initialize all the variables to it.
In other words, I've just reinvented serialization, except that I don't control the backend system and just rather make working with it really painless. I don't want callers to have to call Initialize() after constructing the object, because in 100% of the cases you have to initalize it after constructing.
I don't want to move the initialize code into the Derived Classes, except for string-to-business-object conversion.
Would I have to use a Factory? Or is it considered safe to look at the property names of a derived class in a base constructor? (Don't care about their values and that they aren't initialized, just need the names).
Or is there a better way altogether to provide a facade between a Dictionary of strings and a concrete business object?
Edit: This is .net 3.5, so no System.Dynamic which would make this trivial :(
Edit 2: After looking at the Answers and thinking through this some more, I guess my question really boils down to this now: Is calling GetType().GetProperties() from a base constructor in order to get the Names of Properties and if they are decorated with a certain Attribute safe?
Wait, let's stop here for a second and do this properly. It shouldn't be MyBase's responsibility to do this.
So you write a class that manages getting stuff out of the backend for you, and you write a method on that class that is something like
T Get<T>() where T : new()
and you make Get responsible for reading the dictionary out of the backend and using reflection to populate an instance of T. Thus, you say
var concrete = foo.Get<MyConcrete>();
This isn't hard, and it's the right way to do it.
Incidentally, the code for Get is going to look something like
T t = new T();
var properties = typeof(T).GetProperties();
foreach(var property in properties) {
property.SetValue(t, dictionary[property.Name], null);
}
return t;
where dictionary is your loaded up key/value pairs. It turns out there are more optimal ways to do this, but unless it's a bottleneck I wouldn't worry about it.
The better way to do this would be to make the classes use the dictionary directly:
public string Hello {
get { return (string)base.data["Hello"]; }
set { base.data["Hello"] = value; }
}
You may want to call TryGetValue in the getter so that you can return a default value if the key isn't there. (You should probably do that in a separate method in the base class)
You can make a code snippet to make the properties easier to create.
If you don't want to do it this way, you can call GetType().GetProperties() to get PropertyInfo objects for the properties in your class, then call SetValue(this, value).
This will be slow; there are various tricks you can use to speed it up using expression trees, CreateDelegate, or IL generation.
Maybe try the Template method pattern
Have you considered using an ExpandoObject? With it you can dynamically add properties and inspect them (when serializing for example).
I'm not sure if it's what you really should do, but here's what you asked for (put this in Initialize, and you'll get a list of the derived property names):
var derivedProps = this.GetType().GetProperties();
var propNames = new List<string>(derivedProps.Select(x => x.Name));
From there, using the PropertyInfos in derivedProps, you can set the properties.
You can't really safely do anything to those properties in the base-class constructor anyway as some derived constructor may reset them anyway. You're much better off doing a two-phased load (e.g. call Initialize explicitly)

Return all fields in an instance of a derived class

I'm looking for a way to return the value of every field in an object in the preferably in the form of a list, but any collection type will work. After a bit of researching, I found this code
FieldInfo[] fields = this.GetType().GetFields();
foreach(FieldInfo field in fields)
// Perform actions
Would this be the correct way to handle this? Also, if I put this method in the base class, if a derived class calls it will it return all the fields in the derived class or the base class? (Or more simply put would I need to override this for each derived class?)
It's not a problem to do it individually for each class if that's the only way, but I'm just looking to save a bit of code, since I'm going to have some 50+ derived classes.
You're using GetType(), so that will return the fields of the actual object type, not the type you're declaring the method in. You don't need to override
However, your code will currently only get public fields - and I hope you don't have any :)
You probably want something like:
var fields = GetType().GetFields(BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public);
...
I believe that will include inherited fields (as it's not specifying BindingFlags.DeclaredOnly) but you should check.
Yes, this is a correct way of handling this.
this.GetType() whill return the actual type of the object, so if it is a derived class it will return the type of the derived class and it will work as expected. So: No you do not have to override it in each child class.
One thing: GetType() returns only the public fields. If you also want other fields then you need to use the overload GetType(BindingFlags) and specify the appropriate flags.

Generic ComboBox with automatically named properties

I'm writing a wrapper for the WinForms ComboBox control that will let me populate the dropdown with a List<T>, and has a Selected property that returns an item of type T (or null if nothing selected).
Rather than having a Selected property, I'd like it to be named based on the generic type automatically. For example:
MyDropDownList<User> would have a SelectedUser property
MyDropDownList<Department> would have a SelectedDepartment property
MyDropDownList<State> would have a SelectedState property
With LINQ, I can create anonymous types during grouping, like so:
var usersByGender = users
.GroupBy(x => x.Gender)
.Select(group => new {Gender = group.Key, List = group.ToList()});
Which will result in a list of the generated anonymous type containing a Gender property and List<User> that are detectable by IntelliSense. The question is, can I somehow do this with properties in a generic class?
EDIT: I now realize I'm essentially asking for "method_missing" in C# 3.0, which is probably impossible. I'm open to suggestions, though.
I don't think you can do it. The only approach that has even a remote chance of working would be to create some kind of factory method that created the type and returned it. Basically your generic type becomes a base class and IL is dynamically written creating a concrete class that inherits from the generic but with the new property added in. Of course the object that is returned would be really anonymous and you'd only be able to access it through reflection since you wouldn't have a type to actually cast to:
public static class CrazyFactory
{
public static object CreateTypedComboList<T>()
{
//magic happens
return object;
}
}
Using it would then require
object userCombo = CrazyFactory.CreateTypedComboList<User>();
PropertyInfo selectedUserProperty = userCombo.GetType().GetProperty("SelectedUser");
selectedUserProperty.SetValue(userCombo, user);
Which is hardly a step up from a simple T SelectedItem property.
From a brief look it appears that this type of thing will be easier when c# 4.0 brings us dynamic types. By implementing the IDynamicObject interface it will be possible to catch all calls to undefined properties and handle them so you could use logic like the following:
public override MetaObject GetMember(GetMemberAction action, MetaObject[] args)
{
//not sure on the real property name here...
string actionName = action.Name;
if (actionName = "Selected" + typeof(T).Name)
{
return SelectedItem; //in some MetaObject wrapper
}
}
I've been following this article and haven't touched dynamics myself yet, but it certainly appears like what you want is possible. I still wouldn't do it though - you won't get Intellisense and it seems like a fragile and complicated method for defining functionality.
No, you cannot do that. The whole idea behind generic types is that they apply equally to all types which can be specified in the generic parameter. This wouldn't be the case if every specialization were allowed to declare differently-named members.

Working with a hashtable of unknown but similar objects (C#)

I have a hash table which can contain any number of objects. All of these objects implement some similar methods / properties and some of their own.
For example all objects in the hashtable may have a method called PrintText taking a single parameter of type string. All the objects are however instantiated from different classes.
Is it possible for me to pull out a particular object from the hashtable by its key without knowing its type before runtime, and access all its own methods and properties (not just the common ones)?
Normally I would do something like,
MyClass TheObject = MyHashTable[Key];
But the object being pulled out could be derived from any class so I cannot do that in this instance.
You could define an interface containing the common methods and properties, and implement this interface in all your classes. Then you can easily access these methods and properties.
But to access the specific methods of an object (not contained in the interface), you will need to know the type of the object.
Update:
It's not clear from your question, but when you write about a hashtable, I assume you mean the Hashtable class. In that case, you should have a look at the generic Dictionary class (available since .NET 2.0). This class will make your code typesafe and saves you from a lot of type-casting, e.g:
IMyInterface a = new MyObject();
// with Hashtable
Hashtable ht = new Hashtable();
ht.Add("key", a);
IMyInterface b = (IMyInterface)ht["key"];
// with Dictionary
var dic = new Dictionary<string, IMyInterface>();
dic.Add("key", a);
// no cast required, value objects are of type IMyInterface :
IMyInterface c = dic["key"];
To solve problems with common methods and properties you can solve by making your classes to implement the same interface. However, I don't see how you can access non-common members. You can try to use Reflection.
dynamic in C# 4. Reflection in earlier versions.
EDIT: In some cases, defining a common interface can be both an efficient and clear way of achieving something of the nature you describe. ('inspired' by the accepted answer and/or others mentioning it - can't remember the timeline)
You can say:
object theObject = MyHashTable[Key];
Then you can say:
theObject.GetType()
.GetMethod("PrintText")
.Invoke(theObject, new object[] {"paramvalue"});

Categories

Resources