[英]Loading a concrete type of a generic class using reflection
Basically we had some code from a older version of our product which used XML to drive the loading of external code. 基本上,我们从产品的旧版本中获得了一些代码,这些代码使用XML来驱动外部代码的加载。
eg 例如
ObjectHandle handle = Activator.CreateInstance(
information.AssemblyName,
information.TypeName);
loadedObject = (T)handle.Unwrap();
However, this fails when attempting to load a type which has a generic parameter on it. 但是,当尝试加载具有通用参数的类型时,此操作将失败。 Now I do know what the type is going to be at compile time (likely that the type will also be external and may change depending on situation (in the xml only)). 现在,我知道类型在编译时将是什么(可能该类型也将是外部类型,并且可能会根据情况而改变(仅在xml中))。
Is there a way of loading a class of type: where T is of type ActionSettings 有没有一种加载类型的类的方法:其中T是ActionSettings类型的
public class MockTestRunner<T> : IRunner<T> where T : class
{
#region IRunner<T> Members
public T Run(string runnerXml)
{
MvcActionSettings mvcActionSettings = XmlSerialiser.XmlDeserialise<MvcActionSettings>(runnerXml);
IMvcActionSettingsCreator creator = new MockPassThroughActionSettingsGenerator();
var v = creator.Create(mvcActionSettings);
return v as T;
}
public void Initialise(IWizardManagerBase manager)
{
}
}
/// <summary>
/// An MVC controller settings object.
/// </summary>
[Serializable]
public class ActionSettings
{
/// <summary>
/// Initializes a new instance of the ActionSettings class.
/// </summary>
public ActionSettings()
{
PartialViews = new List<PartialViewEntity>();
}
public ActionSettings(bool endOfWizard)
{
EndOfWizard = endOfWizard;
}
public bool EndOfWizard
{
get;
set;
}}
Regards, Jamie 此致杰米
Meh, overcomplicated as always. 嗯,一如既往的复杂。 Didn't realise I should do: 没意识到我应该做:
public class MockTestControllerRunner : IRunner<Interfaces.ActionSettings>
{
#region IRunner<T> Members
public ActionSettings Run(string runnerXml)
{
MvcActionSettings mvcActionSettings = XmlSerialiser.XmlDeserialise<MvcActionSettings>(runnerXml);
IMvcActionSettingsCreator creator = new MockPassThroughActionSettingsGenerator();
Interfaces.ActionSettings v = creator.Create(mvcActionSettings);
return v;
}
#endregion
#region IRunnerWorkflowSubscriber Members
public void Initialise(IWizardManagerBase manager)
{
}
#endregion
}
This removes the need for finding away around the generic parameter issue with reflection. 这消除了寻找与反射有关的通用参数问题的需要。
Regards, Jamie 此致杰米
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.