如何在System.Type对象中创建类型为storead的对象? [英] How to create an object of a type storead in a System.Type object?

查看:84
本文介绍了如何在System.Type对象中创建类型为storead的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有这个代码:

Hi all,

I''ve this code:

Type interfaceType = typeof(ScriptInterface.IBaseScript);

ScriptInterface.IScriptType1 scriptObject = constructor.Invoke(null) as ScriptInterface.IScriptType1;

/*i want to make it more generic such:

(interfaceType) scriptObject = constructor.Invoke(null) as interfaceType; 
*/



如何在下面将"scriptObject"创建为"interfaceType"类型?

谢谢.



How can i create the "scriptObject" below as "interfaceType" type?

Thanks.

推荐答案

通常,您可以使用反射按类型创建实例.在这里看看:
http://msdn.microsoft.com/en-us/library/5b1f63by.aspx [ ^ ]

另外,请检查以下内容:
http://stackoverflow.com/questions/2198989/creating-a-generic- ilist-instance-using-reflection [ ^ ]

祝您好运!
Normally you can use reflection to create an instance by type. Have a look here:
http://msdn.microsoft.com/en-us/library/5b1f63by.aspx[^]

Also, check this:
http://stackoverflow.com/questions/2198989/creating-a-generic-ilist-instance-using-reflection[^]

Good luck!


首先,您需要确保某种类型实现了所需的接口.
System.Type.GetInterface(string)与参数typeof(ScriptInterface.IBaseScript).FullNameSystem.Type.FindInterfaces一起使用.

如果实现了接口,则接下来查找所需的构造函数.最好使用System.Type.GetConstructors(BindingFlags)查找所有构造函数.对于绑定标志,请确保使用System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic,因为您可能还需要使用私有或内部构造函数.它将返回类型为System.Reflection.ConstructorInfo的对象的数组.

最后,如果找到所需的构造函数类型(检查System.Reflection.ConstructorInfo的属性以发现具有可以使用的签名的构造函数),请使用方法System.Reflection.ConstructorInfo之一构造对象.它将返回一个对象.您需要将其强制类型转换为ScriptInterface.IBaseScript才能使用.

就这样!

祝你好运,
—SA
First, you need to make sure some type implements the interface you need.
Use System.Type.GetInterface(string) with a parameter typeof(ScriptInterface.IBaseScript).FullName or System.Type.FindInterfaces.

If the interface is implemented, on a next, find out a constructor you expect. It''s the best to find all constructors using System.Type.GetConstructors(BindingFlags). For binding flags make sure you use System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic, because you may need to use private or internal constructor as well. It will return you an array of the objects of the type System.Reflection.ConstructorInfo.

Finally, if you find the constructor type you expect (check up the properties of System.Reflection.ConstructorInfo to find out you have the constructor with the signature you can use), construct the object using one of the methods System.Reflection.ConstructorInfo. It will return you an object. You need to type cast it to ScriptInterface.IBaseScript so you could use it.

That''s it!

Good luck,
—SA


让我在这里举一个小例子来尝试解释.

公共课Sample
{
公共Sample()
{
}

公共无效SampleMethod()
{
Console.WriteLine(这是示例方法!");
}
}

main()
{
//在这里输入类型.
System.Type类型= typeof(Sample);

//使用反射创建一个对象
//您可以以任何一种方式使用它
动态obj = System.Reflection.Activator.CreateInstance(type,null);

//访问成员
obj.SampleMethod();
}

在上面的示例中,我们使用Activator.CreateInstance创建特定类型的对象并访问该类型的成员.

这是您问题的答案吗?
Let me try to explain by taking a tiny example here.

public class Sample
{
public Sample()
{
}

public void SampleMethod()
{
Console.WriteLine("This is sample method !");
}
}

main()
{
// Taking a type here.
System.Type type = typeof(Sample);

// Creates an object using reflection
// You can use it in either way
dynamic obj = System.Reflection.Activator.CreateInstance(type, null);

// Access a member
obj.SampleMethod();
}

In above example, We have used Activator.CreateInstance to create an object of particular type and access the members on that type.

Is this an answer to your question?


这篇关于如何在System.Type对象中创建类型为storead的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆