I have a dictionary that serves as a type mapper with C #.
var Map = new Dictionary<Type, Type>()
{
{typeof(A1), typeof(A2)},
{typeof(B1), typeof(B2)},
{typeof(C1), typeof(C2)},
{typeof(D1), typeof(D2)}
};
I intend to use it as follows:
public virtual T Get<T>(int id) where T : CD.IDomainModel
{
using (var rep = new R.Rep())
{
var t = PegarEquavalencia(typeof(T));
var obj = rep.Get<t>(id);//Aqui aponta um erro no 't'
return (T)Activator.CreateInstance(typeof(T), obj);
}
}
public static Type PegarEquavalencia(Type type)
{
Type tipoEquivalente;
Map.TryGetValue(type, out tipoEquivalente);
return tipoEquivalente;
}
The problem is that I have not yet been able to use this Type Map to be able to do this action. Basically what I want to do is pass an A1 type, take its equivalent in the dictionary, in case A2, and use the A2 type as a parameter for the generic type of the other method.
I'm using NHibernate, and the Rep class is a generic repository. The Get<T>(object id)
method has the generic type to return the object I want to get in the database.
My solution basically has 3 projects:
- One for database mapping, Domain, Mapping (xml) and Repository;
- One for UI;
- And the other to the business logic of the application.
In the project that encapsulates business logic, I have objects that will be used to exchange data with the UI.
I made a dictionary as above to map objects for UI communication with mapping objects with ORM.
What I want to do is to request an object of type A1, from the interface, there an intermediate method will check its equivalent in the dictionary, search the object with the equivalent type, A2, in the database, and return object A1 which receives with parameter the object A2;