2010-12-21 8 views
0

기본적으로 우리는 XML을 사용하여 외부 코드로드를 유도하는 이전 버전의 코드를 가지고있었습니다.리플렉션을 사용하여 구체적인 유형의 일반 클래스로드

예컨대

ObjectHandle handle = Activator.CreateInstance(
             information.AssemblyName, 
             information.TypeName); 

loadedObject = (T)handle.Unwrap(); 

그것에 일반적인 매개 변수가있는 형식을로드 할 때 그러나,이 실패합니다. 이제 형식이 컴파일 타임에 어떻게 될지 알 수 있습니다 (형식이 외부 일 수도 있고 상황에 따라 달라질 수도 있습니다 (XML 만 해당)). 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; 
    }} 

감사합니다, 제이미

+0

http://stackoverflow.com/questions/266115/pass-an-instantiated-system-type-as-a-type-parameter-for-a-generic-class –

+0

하지 제이미 그렇다면 Type 매개 변수는 실행중인 어셈블리 또는 프레임 워크 내에서 명시 적으로 발생합니다. 외부 형식을로드해야합니다. – Jamie

답변

1

시큰둥, 항상 overcomplicated입니다 :

타입의 클래스를 로딩하는 방법이 있나요. 몰랐 I 해야 수행

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 
} 

이것은 반사와 일반 매개 변수 문제를 해결 떨어진 발견에 대한 필요성을 제거합니다.

감사합니다,

관련 문제