是否可以获取对象的类型并将其动态设置为对象。我有几个 ViewModels 都包含相同的属性并想做这样的事情
if (this.DataContext is CamerasViewModel)
{
//Type type = Type.GetType((this.DataContext.ToString());
object o = Assembly.GetExecutingAssembly().CreateInstance(this.DataContext.GetType().ToString());
Type type = o.GetType();
foreach (ButtonViewModel button in (this.DataContext as type).Buttons)
{
if (button.DisplayName == this.Content.ToString())
{
this.Template = (ControlTemplate)this.FindResource(button.TemplateResource.Substring(0, button.TemplateResource.Length - 3) + "pr");
break;
}
}
}
而不是说 this.DataContext 作为 CamerasViewModel 我想说 this.DataContext 作为 THEDYNAMICTYPE
有什么建议么?