草庐IT

class_createInstance

全部标签

c# - 公共(public)静态字符串 MyFunc() 上的 "Expected class, delegate, enum, interface or struct"错误。什么 's an alternative to "字符串”?

当我尝试使用以下静态函数时出现错误。错误:Expectedclass,delegate,enum,interface,orstruct函数(和类):namespaceMyNamespace{publicclassMyClass{//SomeotherstaticmethodsthatuseClasses,delegates,enums,interfaces,orstructspublicstaticstringMyFunc(stringmyVar){stringmyText=myVar;//DosomestuffwithmyTextandmyVarreturnmyText;}}}这导致

c# - 我可以将 Activator.CreateInstance 与接口(interface)一起使用吗?

我有一个例子:Assemblyasm=Assembly.Load("ClassLibrary1");Typeob=asm.GetType("ClassLibrary1.UserControl1");UserControluc=(UserControl)Activator.CreateInstance(ob);grd.Children.Add(uc);我正在创建类的实例,但是如何创建实现某个接口(interface)的类的实例?即UserControl1实现了ILoad接口(interface)。U:我可以稍后将对象转换为接口(interface),但我不知道程序集中的哪个类型实现了该接

c# - 具有私有(private)密封类的 Activator.CreateInstance

我正在尝试新建一个LocalCommand实例,它是System.Data.SqlClient.SqlCommandSet的私有(private)类。我似乎能够很好地获取类型信息:AssemblysysData=Assembly.Load("System.Data,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089");localCmdType=sysData.GetType("System.Data.SqlClient.SqlCommandSet+LocalCommand");但是当我尝试实例化Activat

c# - OOPS 概念 : What is the difference in passing object reference to interface and creating class object in C#?

我有一个类CustomerNew和一个接口(interface)ICustomer:publicclassCustomerNew:ICustomer{publicvoidA(){MessageBox.Show("Classmethod");}voidICustomer.A(){MessageBox.Show("Interfacemethod");}publicvoidB(){MessageBox.Show("ClassMethod");}}publicinterfaceICustomer{voidA();}我对这两行代码很困惑。ICustomerobjnew=newCustomerNe

c# - ASP.net Web API : change class name when serializing

我有一个产品的数据传输对象类publicclassProductDTO{publicGuidId{get;set;}publicstringName{get;set;}//Otherproperties}当Asp.net序列化JSON(使用JSON.NET)或XML中的对象时,它会生成ProductDTO对象。但是,我想在序列化期间更改名称,从ProductDTO到Product,使用某种属性:[Name("Product")]publicclassProductDTO{[Name("ProductId")]publicGuidId{get;set;}publicstringName{

c# - 低级差异 : non-static class with static method vs. 静态类与静态方法

我想知道使用具有静态方法的非静态类与具有相同静态方法的静态类的一般好处(或缺点)是什么,除了我不能使用非静态类中的静态方法作为扩展方法。例如:classNonStaticClass{publicstaticstringGetData(){return"Thiswasinvokedfromanon-staticclass.";}}与此相比:staticclassStaticClass{publicstaticstringGetData(){return"Thiswasinvokedfromastaticclass.";}}使用一种方法优于另一种方法对性能/内存有何影响?注意:假设我不需要

c# - 使用 Assembly.GetType ("MyCompany.Class1.Class2") 返回 null

我正在尝试使用Assembly.GetType("MyCompany.Class1.Class2")从字符串中动态获取类型。Assembly.GetType("MyCompany.Class1");按预期工作。如果我将一个类嵌入到另一个类中,例如:namespaceMyCompany{publicclassClass1{//.....publicclassClass2{//.....}}}并尝试获取类型Class2Assembly.GetType("MyCompany.Class1.Class2")将返回一个空值。我正在使用.NETFrameworks3.5SP1有谁知道我做错了什么以

c# - 为什么这些是 "Special Classes"?

看完thisquestion问“特殊类”到底是什么,我剩下的问题是为什么这六个类System.Object、System.Array、System.Delegate、System.Enum和System.ValueType被选中并且hard-coded作为特殊类,防止它们被用作泛型类或方法的约束。很容易理解为什么System.Object在那里;所有类都继承System.Object,因此无需将其作为约束包含在内。我不清楚的是,为什么其他人被选为这个特殊类(class)类别的一部分。PS:当尝试将特殊类用作约束时,特殊类会引发编译错误CS0702。 最佳答案

c# - 给定 "where T : new()", "new T()"是否在内部使用 Activator.CreateInstance?

如果我有一个类型参数约束new():voidFoo()whereT:new(){vart=newT();}newT()是否会在内部使用Activator.CreateInstance方法(即反射)? 最佳答案 是的,这是真的。编辑2:这里很好地解释了方法和原因。http://www.simple-talk.com/community/blogs/simonc/archive/2010/11/17/95700.aspx为了验证我编译了如下方法:publicstaticTCreate()whereT:new(){returnnewT()

c# - 我的控件是 "not allowed here because it does not extend class ' System.Web.UI.UserControl'”

所以我有另一个刮面器(无论如何对我来说)。我正在尝试在CMS中创建我自己的自定义控件,我只有部分源代码(即供应商提供给我的示例)。基本上,我创建了一个名为DataDefinitionContent的类,它扩展了ControlBase。现在,根据我从元数据中获得的信息,ControlBase扩展了UserControl,所以我原以为这不会发生什么戏剧性的事情。谁能阐明为什么这对我不起作用?我的类(class):publicpartialclassDataDefinitionContent:ControlBase,ICustomControl{...Stuff}控制库:usingSyste