I have a method that has a constraint like this:
public class MappingTransformation
{
public static ClassMapped<T> Convert<T>(Mapping<T> source) where T : class
{
return ClassMapped<T>.GetInstance(source);
}
}
This T MUST be reference type because it will be passed to EntityTypeConfiguration.
But, when consuming MappingTransformation.Convert<>(Mapping source) I only have a Type and don't know how to convert this Type into the needed "reference type".
Here is how I'm trying to consume it:
#region Test
foreach (var item in mappingAssembly.GetTypes())
{
var mappingObj = Activator.CreateInstance(item);
var modelName = mappingObj.GetType().GetProperty("ModelName").GetValue(mappingObj);
var modelTypeEquivalent = modelAssembly.GetTypes().First(x => x.Name.Equals(modelName));
var convertido = MappingTransformation.Convert<Model.Clientes>((Mapping<Model.Clientes>)mappingObj);
var breakpoint = true;
}
#endregion
How to achieve this?
I think I understand.
It looks like you want to define the type of your generic function at runtime, something like this?
You want to find the type and then pass it as the type for your generic, is that right?
That's actually trickier than you might think. Types applied to generic functions and classes must be defined at compile time, not run time.
To do what you want, you'll need to use more reflection, see this related question for an example.