Generic method to map different object - c#

I would like to create a generic method that can convert an object to a different type. I could not find any solutions for this scenario. Is this possible in C#?
class A
{
public string Name { get; set; }
}
class A1: A
{
public string ConnectionString { get; set; }
}
class B
{
public string DBName { get; set; }
public string DBConnectionString { get; set; }
}
void Main()
{
A obj = //data
var res = APIToBackEndModel<A, B>(obj);
//Do something
A1 obj1 = //data
var res1 = APIToBackEndModel<A1, B>(obj1);
}
private TResult APIToBackEndModel<T, TResult>(T objTo)
{
(TResult)DBName = (T)objTo.Name;
//OR
var obj = new TResult
{
DBName = (T)objTo.Name
}
}
This did not help me.

You won't be able to do it completely generic, as you rely on certain properties to exist. But you knowing these properties exist, is not enough. You have to guarantee the compiler, they exist. You can do this with Constraints on type parameters. With their help, you can define, that the generic types you use, will have certain properties (for instance implement an interface or inherit from a class).
interface InterfaceIn {
string p1 {get;set;}
void m1();
}
interface InterfaceOut {
string p2 {get;set;}
void m2();
}
class ConcreteIn : InterfaceIn {
public string p1 {get;set;}
public void m1() {}
}
class ConcreteOut1 : InterfaceOut {
public string p2 {get;set;}
public void m2() {}
}
class ConcreteOut2 : InterfaceOut {
public string p2 {get;set;}
public void m2() {}
}
class Program
{
static void Main(string[] args)
{
var a = new ConcreteIn{p1 = "some value"};
var b = mapIt<ConcreteIn, ConcreteOut1>(a);
var c = mapIt<ConcreteIn, ConcreteOut2>(a);
}
public static V mapIt<U, V>(U val) where U: InterfaceIn where V: InterfaceOut, new() {
var res = new V {p2 = val.p1};
return res;
}
}
Depending on how much properties and combinations you have, this may be enough. Or you may be better off with something like Automapper as Hans Kesting suggested. Or, if you can't group together some mappings, you will have to implement every mapping yourself.

Related

Cast generic class object to non-generic

I have 2 classes:
public class GenericClass<T>
{
public T Item {get;set;}
}
public class StringClass
{
public string Item {get;set;}
}
now i have a GenericClass object and i need to cast it to StringClass object:
var genericObj = new GenericClass<string>();
var stringObj = (StringClass)genericObj; // <---
How to cast / convert from generic class to specific one?
You can't cast one type to the other because the types are unrelated.
You could, however, define a conversion operator:
public class StringClass
{
public string Item { get; set; }
public static explicit operator StringClass(GenericClass<string> generic)
=> new StringClass { Item = generic.Item };
}
Which would allow this syntax:
var genericObj = new GenericClass<string>();
var stringObj = (StringClass)genericObj;
You can't. You would need common inherited type or implement an interface.
With inheritance:
public class GenericClass<T>
{
public T Item {get;set;}
}
public class StringClass : GenericClass<string>
{
}
if your really need it, you can do this way for examle
var stringObj = new StringClass(genericObj);
public class StringClass
{
public string Item { get; set; }
public StringClass(GenericClass<string> genericClass)
{
Item=genericClass.Item;
}
public StringClass(){}
}
or this is more flexible
public interface IGenericClass<T>
{
public T Item { get; set; }
}
public class GenericClass<T>:IGenericClass<T>
{
public T Item { get; set; }
}
public class StringClass
{
public string Item { get; set; }
public StringClass(IGenericClass<string> genericClass)
{
Item=genericClass.Item;
}
public StringClass(){}
}
Using this answer:
var genericObj = new GenericClass<string>();
var stringObj = (StringClass)Convert.ChangeType(genericObj, typeof(StringClass));
Finally i solved using ICloneable,
Here i have a base class named GenericClass, a generic class named GenericClassT, and a class named StringClass.
Inheritance is:
GenericClass <- GenericClassT <- StringClass
Using ICloneable implementation on GenericClass and GenericClassT, adding a CreateObject and CopyTo methods i reach the final goal:
var genericObj = new GenericClass<string>();
var stringObj = (StringClass)genericObj.Clone<StringClass>();
class definitions:
public class GenericClass: ICloneable
{
public string Id {get;set;}
protected virtual ApiRequestResult CreateObject()
{
return new GenericClass();
}
protected virtual void CopyTo(GenericClass obj)
{
obj.Id = Id;
}
public virtual object Clone()
{
var obj = CreateObject();
CopyTo(obj);
return obj;
}
public virtual object Clone<T>() where T: GenericClass
{
var obj = (GenericClass)Activator.CreateInstance(typeof(T));
CopyTo(obj);
return obj;
}
}
public class GenericClass<T>: GenericClass
{
public T Data {get; set;}
protected override GenericClass CreateObject()
{
return new GenericClass<T>();
}
protected override void CopyTo(GenericClass obj)
{
base.CopyTo(obj);
((GenericClass<T>)obj).Data = Data;
}
}
public class StringClass: GenericClass<string>
{
}

Overloading virtual generic method

I'd like to ask if someone can help me.
I have base generic class
public class Base<T> where T : new()
{
public int ID { get; set; }
public string Name { get; set; }
public virtual string Foo()
{
throw new NotImplementedException("");
}
}
Then I have
public class A : Base<A>
{
public override string Foo()
{
return string.Empty;
}
}
and in my main code I would like to do something like:
A entity = new A();
var x = entity.Foo();
List<A> entityList = new List<A>();
var y = entityList.Foo();
my code works for entity and x, but I'd like to overload Foo to be called also on a list. Can someone help?
For such things (when you need to extend existing class without modifying source code of it) you can create extension method, for example
public static class BaseExtensions
{
public static string Foo<T>(this IEnumerable<Base<T>> items) where T : new()
{
var builder = new StringBuilder();
foreach (var item in items)
{
builder.Append(item.Foo());
}
return builder.ToString();
}
}
that concatenates all Foo results of items in array/list.

Get a variable from a class that inherits another

I have an arbitrary amount of classes, classThatInherits, anotherClassThatInherits, etc. that inherit classToBeInherited.
I then have a method, b, that needs to be able to access myValue from the classes that inherit classToBeInherited. How can I achieve this, without casting?
//This class will be inherited by other classes
public class classToBeInherited {
public bool isSomething { get; set; }
}
//This class with inherit 'classToBeInherited'
public class classThatInherits : classToBeInherited {
public int myValue { get; set; } //this needs to be accessable...
}
//...And so will this class
public class anotherClassThatInherits : classToBeInherited {
public int myValue { get; set; }
}
private class normalClass {
private void a() {
classThatInherits cti = new classThatInherits();
b(cti);
anotherClassThatInherits acti = new anotherClassThatInherits();
b(acti);
}
private void b(classToBeInherited c) {
//***
//get myValue from the classes that inherit classToBeInherited
//***
}
}
Move myValue to classToBeInherited:
public class classToBeInherited {
public bool isSomething { get; set; }
public abstract int myValue { get; set; }
}
Then in classThatInherits and anotherClassThatInherits use public override int myValue { get; set; } to implement that property.
Ofcorse, if myValue is needed in only some of the classes, then you can have virtual and not abstract property.
var a = c as anotherClassThatInherits;
if (a != null)
{
var myValue = a.myValue;
}
I don't know why you don't want to do casting, but it's very common to have code like above.
UPDATED
If you really don't want casting, you can use reflection (but you still need to know the type of anotherClassThatInherits)
var getter = typeof(anotherClassThatInherits).GetProperty("myValue").GetGetMethod();
var myValue = getter.Invoke(c, null);

calling derived methods on a baseclass collection

I have one abstract class named A, and other classes (B, C, D, E, ...) that implements A.
My derived classes are holding values of different types.
I also have a list of A objects.
abstract class A { }
class B : class A
{
public int val {get;private set;}
}
class C : class A
{
public double val {get;private set;}
}
class D : class A
{
public string val {get;private set;}
}
class Program
{
static void Main(string[] args)
{
List list = new List { new B(), new C(), new D(), new E() };
// ...
foreach (A item in list)
{
Console.WriteLine(String.Format("Value is: {0}", item.val);
}
}
}
...where the .val is not known by the base-class ofc.
How can i get this dynamic behaviour? I don't want to use getType in a long switch/if-statements.
If you just want to get the string representation of val i recoment overriding ToString in each of the sub classes
public override string ToString()
{
return val.ToString();
}
either way if you want the data in a sub class you need to represent it in the base class as some type they all have in common (like object). and you could do it like this
abstract class A
{
public abstract object GetValue();
}
class B : class A
{
public int val {get;private set;}
public override object GetValue()
{
return val;
}
}
Try this:
using System;
using System.Collections.Generic;
abstract class A
{
public abstract dynamic Val { get; set; }
}
class B : A
{
public override dynamic Val { get; set; }
}
class C : A
{
public override dynamic Val { get; set; }
}
class D : A
{
public override dynamic Val { get; set; }
}
class Program
{
static void Main(string[] args)
{
var list = new List<A> { new B(), new C(), new D() };
// ...
foreach (A item in list)
{
Console.WriteLine(String.Format("Value is: {0}", item.Val));
}
}
}
abstract class A { public string val { get; set; } }
class B : A
{
public int val {get;private set;}
}
class C : A
{
public double val {get;private set;}
}
class D : A
{
public string val {get;private set;}
}
class Program
{
static void Main(string[] args)
{
List<object> list = new List<object> { new B(), new C(), new D() };
// ...
foreach (A item in list)
{
Console.WriteLine(String.Format("Value is: {0}", item.val));
}
}
}
If val is not a member of the base class then you can't access it on a reference of that type. Those three derived classes may all have a member named val but it is NOT the same member so it cannot be treated like it is. What you could do is declare a generic class and make that val property of the generic type but then you couldn't create a List of that type. Basically, what you want to do is not possible. It's not based on inheritance and it's not based on generics. It sounds convenient but it's not logical.

C# - copying property values from one instance to another, different classes

I have two C# classes that have many of the same properties (by name and type). I want to be able to copy all non-null values from an instance of Defect into an instance of DefectViewModel. I was hoping to do it with reflection, using GetType().GetProperties(). I tried the following:
var defect = new Defect();
var defectViewModel = new DefectViewModel();
PropertyInfo[] defectProperties = defect.GetType().GetProperties();
IEnumerable<string> viewModelPropertyNames =
defectViewModel.GetType().GetProperties().Select(property => property.Name);
IEnumerable<PropertyInfo> propertiesToCopy =
defectProperties.Where(defectProperty =>
viewModelPropertyNames.Contains(defectProperty.Name)
);
foreach (PropertyInfo defectProperty in propertiesToCopy)
{
var defectValue = defectProperty.GetValue(defect, null) as string;
if (null == defectValue)
{
continue;
}
// "System.Reflection.TargetException: Object does not match target type":
defectProperty.SetValue(viewModel, defectValue, null);
}
What would be the best way to do this? Should I maintain separate lists of Defect properties and DefectViewModel properties so that I can do viewModelProperty.SetValue(viewModel, defectValue, null)?
Edit: thanks to both Jordão's and Dave's answers, I chose AutoMapper. DefectViewModel is in a WPF application, so I added the following App constructor:
public App()
{
Mapper.CreateMap<Defect, DefectViewModel>()
.ForMember("PropertyOnlyInViewModel", options => options.Ignore())
.ForMember("AnotherPropertyOnlyInViewModel", options => options.Ignore())
.ForAllMembers(memberConfigExpr =>
memberConfigExpr.Condition(resContext =>
resContext.SourceType.Equals(typeof(string)) &&
!resContext.IsSourceValueNull
)
);
}
Then, instead of all that PropertyInfo business, I just have the following line:
var defect = new Defect();
var defectViewModel = new DefectViewModel();
Mapper.Map<Defect, DefectViewModel>(defect, defectViewModel);
Take a look at AutoMapper.
There are frameworks for this, the one I know of is Automapper:
http://automapper.codeplex.com/
http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/01/22/automapper-the-object-object-mapper.aspx
Replace your erroneous line with this:
PropertyInfo targetProperty = defectViewModel.GetType().GetProperty(defectProperty.Name);
targetProperty.SetValue(viewModel, defectValue, null);
Your posted code is attempting to set a Defect-tied property on a DefectViewModel object.
In terms of organizing the code, if you don't want an external library like AutoMapper, you can use a mixin-like scheme to separate the code out like this:
class Program {
static void Main(string[] args) {
var d = new Defect() { Category = "bug", Status = "open" };
var m = new DefectViewModel();
m.CopyPropertiesFrom(d);
Console.WriteLine("{0}, {1}", m.Category, m.Status);
}
}
// compositions
class Defect : MPropertyGettable {
public string Category { get; set; }
public string Status { get; set; }
// ...
}
class DefectViewModel : MPropertySettable {
public string Category { get; set; }
public string Status { get; set; }
// ...
}
// quasi-mixins
public interface MPropertyEnumerable { }
public static class PropertyEnumerable {
public static IEnumerable<string> GetProperties(this MPropertyEnumerable self) {
return self.GetType().GetProperties().Select(property => property.Name);
}
}
public interface MPropertyGettable : MPropertyEnumerable { }
public static class PropertyGettable {
public static object GetValue(this MPropertyGettable self, string name) {
return self.GetType().GetProperty(name).GetValue(self, null);
}
}
public interface MPropertySettable : MPropertyEnumerable { }
public static class PropertySettable {
public static void SetValue<T>(this MPropertySettable self, string name, T value) {
self.GetType().GetProperty(name).SetValue(self, value, null);
}
public static void CopyPropertiesFrom(this MPropertySettable self, MPropertyGettable other) {
self.GetProperties().Intersect(other.GetProperties()).ToList().ForEach(
property => self.SetValue(property, other.GetValue(property)));
}
}
This way, all the code to achieve the property-copying is separate from the classes that use it. You just need to reference the mixins in their interface list.
Note that this is not as robust or flexible as AutoMapper, because you might want to copy properties with different names or just some sub-set of the properties. Or it might downright fail if the properties don't provide the necessary getters or setters or their types differ. But, it still might be enough for your purposes.
This is cheap and easy. It makes use of System.Web.Script.Serialization and some extention methods for ease of use:
public static class JSONExts
{
public static string ToJSON(this object o)
{
var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return oSerializer.Serialize(o);
}
public static List<T> FromJSONToListOf<T>(this string jsonString)
{
var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return oSerializer.Deserialize<List<T>>(jsonString);
}
public static T FromJSONTo<T>(this string jsonString)
{
var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return oSerializer.Deserialize<T>(jsonString);
}
public static T1 ConvertViaJSON<T1>(this object o)
{
return o.ToJSON().FromJSONTo<T1>();
}
}
Here's some similiar but different classes:
public class Member
{
public string Name { get; set; }
public int Age { get; set; }
public bool IsCitizen { get; set; }
public DateTime? Birthday { get; set; }
public string PetName { get; set; }
public int PetAge { get; set; }
public bool IsUgly { get; set; }
}
public class MemberV2
{
public string Name { get; set; }
public int Age { get; set; }
public bool IsCitizen { get; set; }
public DateTime? Birthday { get; set; }
public string ChildName { get; set; }
public int ChildAge { get; set; }
public bool IsCute { get; set; }
}
And here's the methods in action:
var memberClass1Obj = new Member {
Name = "Steve Smith",
Age = 25,
IsCitizen = true,
Birthday = DateTime.Now.AddYears(-30),
PetName = "Rosco",
PetAge = 4,
IsUgly = true,
};
string br = "<br /><br />";
Response.Write(memberClass1Obj.ToJSON() + br); // just to show the JSON
var memberClass2Obj = memberClass1Obj.ConvertViaJSON<MemberV2>();
Response.Write(memberClass2Obj.ToJSON()); // valid fields are filled
For one thing I would not place that code (somewhere) external but in the constructor of the ViewModel:
class DefectViewModel
{
public DefectViewModel(Defect source) { ... }
}
And if this is the only class (or one of a few) I would not automate it further but write out the property assignments. Automating it looks nice but there may be more exceptions and special cases than you expect.
Any chance you could have both classes implement an interface that defines the shared properties?

Categories

Resources