I want to create a method that will accept any entity framework database model I've created and return me back another object of the same type. How do I create another object of the same type that I pass to this method?
public object CopyDBObject(object a) {
Type type = typeof(a).MakeGenericType(a.GetType());
object a1 = Activator.CreateInstance(type);
PropertyInfo[] propertys = a.GetType().GetProperties();
foreach (var property in propertys)
{
//iterate through properties
//set a1 properties equal to several a properties
}
return a1; }
Basically I just want "a1" to be the same type as "a" which I'm passing into this method.
At first I just had a line of code:
object a1 = new object();
but I got an error since "a1" was just a generic object, so I couldn't set its properties similar to what "a" has.
So I changed that line to these listed above:
Type type = typeof(a).MakeGenericType(a.GetType());
object a1 = Activator.CreateInstance(type);
but this doesn't work. I don't know what should be in place of "typeof(a)". The error says that it is "a is a variable being used like a type." When I set something to "a.GetType()" it does show me the correct object type for "a" that I passed it, but how do I set "a1" to be that same type?