可能问的不是很明白,解释一下:
比如,我想有一个Create方法,声明成这样了:
object Create(Type t)现在想问的是:怎样定义这个Create以实现当t不同时返回不同的实例。比如传入t=typeof(xxx)就return 一个XXX的实例指教
比如,我想有一个Create方法,声明成这样了:
object Create(Type t)现在想问的是:怎样定义这个Create以实现当t不同时返回不同的实例。比如传入t=typeof(xxx)就return 一个XXX的实例指教
解决方案 »
- C/S模式的客户端服务器操作问题
- 请问一个菜鸟问题
- 如何做客户端的数据检索?
- 求解答,多个xml文档合并的问题~~~~~~~~~~~~~~~~~~~~~~~
- Winform 的 datagrid 能不能添加按钮列啊?
- VB2VB.NET: SQL Server does not exist or access denied
- 請教原因何在???在線等待
- 写了类库,怎么在调用的使用显示类库中文注释
- >>>>>帮忙翻译为E文,包括代码的中文
- 如何根据计算机名字获的该计算机的名字
- 请教关于Excel导入到Access的问题!C#程序实现?
- 如何自定义软件的界面(比如说不用Windows那个框框什么的,用自己切进来的一个界面)???
只不过你return之后还是object的,在其他地方调用Create(t)的时候,返回的还是object类型,你可以进行转换。
如果要想直接return其他类型,我认为是不行的。你的函数的返回类型已经被你定义成object了,你说是吗?
{
Type myType = typeof(MyClass1);
Type[] types = new Type[1];
types[0] = typeof(int);
// Get the public instance constructor that takes an integer parameter.
ConstructorInfo constructorInfoObj = myType.GetConstructor(
BindingFlags.Instance | BindingFlags.Public, null,
CallingConventions.HasThis, types, null);
if(constructorInfoObj != null)
{
Console.WriteLine("The constructor of MyClass1 that is a public " +
"instance method and takes an integer as a parameter is: ");
Console.WriteLine(constructorInfoObj.ToString());
}
else
{
Console.WriteLine("The constructor of MyClass1 that is a public instance " +
"method and takes an integer as a parameter is not available.");
}
}
catch(ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: " + e.Message);
}
catch(ArgumentException e)
{
Console.WriteLine("ArgumentException: " + e.Message);
}
catch(SecurityException e)
{
Console.WriteLine("SecurityException: " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}