我尝试编写Activator动作来改变当前的歌曲评级.我现在可以读取MPMediaItemPropertyRating属性并获得评级.但是我们如何从应用程序中更改它?
我一直在我的一些代码中使用Activator.CreateInstance().使用此实例创建实例是否有任何风险?
假设一个带有参数化构造函数的类如下:
MyObject(string param1, string param2, string param2)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我有一个Generic返回该实例的方法,Generic Class并且
MyObject是其中一个实例.
如何使用参数化构造函数实例化泛型类并将null值传递给其所有参数?这是必需的,因为泛型类的属性会在稍后阶段填充.
我试过Activator用作:
public static T GetGenericObject<T>()
{
T resource = (T)Activator.CreateInstance(typeof(T), null);
...
}
Run Code Online (Sandbox Code Playgroud)
但我得到的例外是我没有参数化的构造函数MyObject.我知道我可以轻松添加一个但是类不能修改(不要问为什么).
我有一个工厂应该创建在运行时从类Foo继承的对象.我认为System.Activator.CreateInstance的返回类型与它创建的对象的类型相同,但从以下错误消息判断,其返回类型是Object.
错误1无法将类型'object'隐式转换为'cs_sandbox.Foo'.存在显式转换(您是否缺少演员表?)F:\ projects\cs_sandbox\Form1.cs 46 24 cs_sandbox
好吧,也许我很缺少强制的,但
return (t)System.Activator.CreateInstance(t);
Run Code Online (Sandbox Code Playgroud)
导致又一条错误信息 - 我必须承认 - 这对我没有意义:
错误1找不到类型或命名空间名称't'(您是否缺少using指令或程序集引用?)F:\ projects\cs_sandbox\Form1.cs 45 25 cs_sandbox
这是我的代码:
class Foo { }
class FooChild1 : Foo { }
class FooChild2 : Foo { }
class MyFactory
{
public static Foo CreateInstance(string s)
{
Type t;
if (s.StartsWith("abcdef"))
{
t = typeof(FooChild1);
return System.Activator.CreateInstance(t);
}
else
{
t = typeof(FooChild2);
return System.Activator.CreateInstance(t);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何修复此代码?或者,如果它不可修复,那么在运行时创建从特定类继承的对象的其他方法是什么?
我正在调用Resolve的异常:
KernelException: Could not instantiate custom activator
Inner Exception:
{"Constructor on type 'MyProj.MyAdapter`1[[MyProj.MyBusinessObject, MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' not found."}
Run Code Online (Sandbox Code Playgroud)
肯定有一个公共无参数构造函数(我在运行时使用反射验证了这一点)...所以我认为这个问题可能与它是通用的事实有关吗?我已经尝试获取组件模型对象并将RequiresGenericArguments设置为true,但这并没有让我在任何地方.
任何帮助将非常感激!谢谢.
如何将Remoting.ObjectHandle转换为UserControl类型?
我想动态实现UserControl:
UserControl myUserControl = (UserControl)Activator.CreateInstance(Assembly.GetExecutingAssembly().FullName, "Client.IndexView");
Run Code Online (Sandbox Code Playgroud)
错误:无法将'System.Runtime.Remoting.ObjectHandle'类型的表达式转换为'UserControl'
我正在尝试使用以下内容创建自定义用户控件:
var panel = new GenericAccordionPanel<ZoneReport, ZonesPanel, ZonesVM>(myVm.ZonesVm);
Run Code Online (Sandbox Code Playgroud)
GenericAccordionPanel 定义为:
public class GenericAccordionPanel<THeader, TBody, TViewModel> : UserControl
{
public Accordion Accordion { get; set; }
public GenericAccordionPanel(TViewModel vmItem)
{
this.Accordion = new Accordion();
//the constructor for ZoneReport(THeader) takes a ZonesVM (vmItem) as a parameter.
var zr = (THeader)Activator.CreateInstance(typeof(THeader), new { vmItem });
var exp = new Expander { Header = zr };
Accordion.Children.Add(exp);
base.Content = Accordion;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是Activator.CreateInstance失败的原因如下MissingMethodException:
找不到类型'[namespace] .Zones.ZoneReport'的构造函数.
我怎样才能创造出一种意志ZoneReport?
我在使用反射实例化 DLL 文件中定义的类时遇到了一些麻烦。尽管代码看起来不错,但我在 Activator.CreateInstance 上得到了 TargetInvocationException。代码如下(目前只有一个 DLL 可用):
public Comparator()
{
string[] filePaths = Directory.GetFiles(@".\Extensions");
for (int i = 0; i < filePaths.Length; ++i)
{
var asm = Assembly.LoadFrom(Path.GetFullPath(filePaths[i]));
if (asm != null)
{
foreach (Type currType in asm.ExportedTypes)
{
if (!currType.IsAbstract && currType.IsPublic && typeof(SorterBase).IsAssignableFrom(currType))
{
Debug.WriteLine(currType); //outputs: BubbleSort.Sorter
var instance = Activator.CreateInstance(currType); //TargetInvocationException
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我试图实例化的类:
using SortingLibrary;
using SortingFunctions;
namespace BubbleSort
{
public class Sorter : SorterBase //SorterBase is an abstract class defined in …Run Code Online (Sandbox Code Playgroud) 我创建了一个动态方法来创建不同类型的实例,但不确定为什么它在编译时给出了上面提到的错误,我是否还要再次将返回值强制转换为指定的类型?
internal static T GetInstance<T>()
{
dynamic obj = Activator.CreateInstance(typeof(T));
return obj;
}
private Foo f = GetInstance<Foo>();
Run Code Online (Sandbox Code Playgroud) 我有这个工厂类,我想正确测试它。假设我有一个抽象类,它有很多子类(继承)。
正如您在我的 Factory 类中的方法 BuildChild 中看到的,我希望能够在运行时创建子类的实例。我必须能够在运行时创建此实例,因为在运行之前不会知道该类型。而且,我不能在这个项目中使用 Unity(如果是这样,我不会问如何实现这一点)。
这是我想测试的工厂类:
public class Factory
{
public AnAbstractClass BuildChild(Type childType, object parameter)
{
AnAbstractClass child = (AnAbstractClass) Activator.CreateInstance(childType);
child.Initialize(parameter);
return child;
}
}
Run Code Online (Sandbox Code Playgroud)
为了测试这一点,我想找到一种 Mock Activator.CreateInstance 的方法来返回我自己的子类的模拟对象。我怎样才能实现这个目标?或者如果您有更好的方法来做到这一点而不使用 Activator.CreateInstance (和 Unity),如果它更容易测试和模拟,我会接受它!
我目前正在使用 Moq 来创建我的模拟,但由于 Activator.CreateInstance 是静态类的静态方法,我不知道如何执行此操作(我已经知道 Moq 只能创建对象的模拟实例)。
我查看了 Microsoft 的 Fakes,但没有成功(我在理解它的工作原理并找到一些解释良好的示例方面遇到了一些困难)。
请帮我!
编辑:
我需要模拟 Activator.CreateInstance 因为我想强制此方法返回另一个模拟对象。我想要的正确的事情只是存根这个方法(而不是模拟它)。
所以当我像这样测试 BuildChild 时:
[TestMethod]
public void TestBuildChild()
{
var mockChildClass = new Mock(AChildClass);
// TODO: Stub/Mock Activator.CreateInstance to return mockChildClass when called with "type" and "parameter" as follow. …Run Code Online (Sandbox Code Playgroud) activator ×10
c# ×8
generics ×2
reflection ×2
wpf ×2
.net ×1
class ×1
constructor ×1
ipod ×1
microkernel ×1
mocking ×1
moq ×1
mpmediaitem ×1
silverlight ×1