Possible Duplicate:
Pass Type dynamically to <T>
How would I do something like the below?
historyType is not a recognized type in Adapt.
Type historyType = Type.GetType("Domain.MainBoundedContext.CALMModule.Aggregates.LocationAgg." + modifiedEntry.GetType().Name + "History");
ServiceLocationHistory history = adapter.Adapt<ServiceLocation, historyType>(modifiedEntry as ServiceLocation);
historyRepository.Add(history);
Edit:
I ended up doing this:
ServiceLocationHistory history = adapter.GetType()
.GetMethod("Adapt", new Type[] { typeof(ServiceLocation) })
.MakeGenericMethod(typeof(ServiceLocation), typeof(ServiceLocationHistory))
.Invoke(adapter, new object[] { modifiedEntry as ServiceLocation })
as ServiceLocationHistory;
This may or may not be an option for you, but you can always use a DynamicMethod and emit IL that creates your type and returns a
ServiceLocationHistory. I often do this instead of hacky reflection tricks, and it is almost always faster.Otherwise, with reflection you can do: