C#Activator.CreateInstance()抛出“无法创建抽象类”静态类中的异常 [英] C# Activator.CreateInstance() throws "cannot create abstract class" exception in static class

查看:441
本文介绍了C#Activator.CreateInstance()抛出“无法创建抽象类”静态类中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#Activator.CreateInstance()抛出异常无法在我的静态类中创建抽象类异常。



我的代码是



类型t2 = Type.GetType(测试);

对象o2 =(Activator.CreateInstance(t2));

C# Activator.CreateInstance() throws exception cannot create abstract class exception in my static class.

my code is

Type t2 = Type.GetType(test);
Object o2 = (Activator.CreateInstance(t2));

推荐答案

此错误消息的哪些部分可能不清楚?让我们看看...



在第一行,您使用此功能: https://msdn.microsoft.com/en-us/library/w3f99sx1%28v=vs.110%29.aspx [ ^ ]。



我不知道为什么,但你很幸运,这个字符串被认为是一些真正存在的类型。但是什么类型?正如异常所告诉你的那样,抽象的。该怎么办?任何你想要的。 1)保持你的代码原样:它行为正确,给你非常期待的字符串响应; 2)将此字符串更改为表示某些非抽象非静态类名的字符串,3)修改同一个类,使其成为非抽象和非静态。



-SA
What part of this error message can be unclear? Let's see…

In first line, you use this function: https://msdn.microsoft.com/en-us/library/w3f99sx1%28v=vs.110%29.aspx[^].

I have no idea why, but you were lucky enough to have this string recognized as some really existing type. But what type? As the exception tells you, the abstract one. What to do? Whatever you want. 1) leave your code as is: it behaves correctly, giving you quite expected response to your string; 2) change this string to the string representing some non-abstract non-static class name, 3) modify the same class, make it non-abstract and non-static.

—SA


这篇关于C#Activator.CreateInstance()抛出“无法创建抽象类”静态类中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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