0
的我有這樣動態創建一個類泛型類型
public class ViewModelBase<T> : Notifier
{
private readonly string _tabHeaderPath;
private readonly T _view;
public ViewModelBase(T view)
{
_view = view;
}
public ViewModelBase(T view, string tabHeaderPath)
{
_view = view;
_tabHeaderPath = tabHeaderPath;
}
public T View
{
get { return _view; }
}
public string TabHeaderPath
{
get { return _tabHeaderPath; }
}
}
一類我想要得到的這種使用反射像這樣的對象。
object PresenterBase= null;
Assembly assembly = Assembly.GetExecutingAssembly();
object objView = null;
// Walk through each type in the assembly looking for our class
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass == true)
{
if (type.FullName.EndsWith("." + "GeneralEnquiryViewModel"))
{
// create an instance of the object
foreach(Type objType2 in assembly.GetTypes())
{
if(objType2.IsClass==true)
{
if (objType2.FullName.EndsWith("." + "GeneralEnquiryView"))
{
objView = Activator.CreateInstance(objType2);
break;
}
}
}
ConstructorInfo ctor = type.GetConstructor(new[] { typeof(GeneralEnquiryView), typeof(string) });
PresenterBase= ctor.Invoke(new object[] { objView, "GeneralEnquiry" });
}
}
}
我可以這樣做:
ViewModelBase<GeneralEnquiryView> objPresenter = GeneralEnquiryViewModel(PresenterBase);
但我想更換GeneralEnquiryView到objview和GeneralEnquiryViewModel到PresenterBase讓我得到objPresenter的一個實例。
嗨尤瓦,我能夠創造presenterBase的obejct但如何將其轉換爲通用類ViewModelBase.or的,我應該接近另一種方式 –