草庐IT

reflect.TypeOf

全部标签

c# - 序列化 bool ?错误反射(reflect)类型

我有一个类似的类[Serializable]publicclassMyClass{[XmlAttribute]publicboolmyBool{get;set;}}但是当xml中不存在属性时,这会将bool的值序列化为false。当属性不在xml中时,我希望该属性为null。所以我试过了[Serializable]publicclassMyClass{[XmlAttribute]publicbool?myBool{get;set;}}但是随后序列化器出错Typet=Type.GetType("Assembly.NameSpace.MyClass");XmlSerializermySer

c# - NancyFX 立即反射(reflect)静态内容的变化

在ASP.NET中,每当我从VS2012以Debug模式运行我的服务器时,我对静态内容(js、css等)所做的任何更改都会在保存时立即反射(reflect)出来。在NancyFX中,我每次更改静态内容时都需要重新启动服务器。我假设这是因为每次运行服务器时VS都需要将静态内容复制到输出目录。有没有办法在保存时立即反射(reflect)对静态内容所做的更改?这是我的静态内容配置publicclassMainBootstrapper:DefaultNancyBootstrapper{protectedoverridevoidConfigureConventions(NancyConventi

c# - 当它发生在Windows Phone中的System.Windows.ni.dll中发生了类型为 "' System.Reflection.TargetInvocationException'的未处理异常

我正在创建一个WP8应用程序,它使用Web服务来获取、创建、更新和删除数据并显示它。现在的问题是我的应用程序因抛出而崩溃Anunhandledexceptionoftype"'System.Reflection.TargetInvocationException'occurredinSystem.Windows.ni.dll"inWindowsPhone此异常没有堆栈跟踪,我被这个异常困了几个小时。我注意到每当调用服务比正常情况更频繁时就会发生此异常,但我没有得到真正的原因。了解真的很有帮助1.这是什么类型的异常?2.在什么情况下会发生这种情况?3.我们如何处理由于此异常导致的应用程序

c# - 为什么 VS 警告我 typeof(T) 永远不是泛型方法中提供的类型,其中类型参数仅限于实现 T?

我希望问题是正确的,所以让我们举个例子。想象以下通用方法:publicabstractclassBase:IDisposable{publicstaticIEnumerableGetList()whereT:Base{//ToensureTinheritsfromBase.if(typeof(T)isBase)thrownewNotSupportedException();//...}}根据MSDN关键字where将类型参数T限制为Base类型或从此类继承。[...]awhereclausecanincludeabaseclassconstraint,whichstatesthatat

c# - 为什么 C# 不允许将 typeof 作为默认参数?

classMyClass{publicvoidMyMethod(TypetargetType=typeof(MyClass)){}}typeof(MyClass)不是编译时常量吗? 最佳答案 我不是IL专家,但它似乎在L_0005调用了一个方法:returntypeof(int);这与:.maxstack1.localsinit([0]class[mscorlib]System.Typetypeofvar)L_0000:ldtokenint32L_0005:callclass[mscorlib]System.Type[mscorli

c# - 将 "C# friendly type"名称转换为实际类型 : "int" => typeof(int)

我想得到一个System.Type给定一个string指定一个(原始)类型的C#友好名称,基本上是C#编译器在阅读C#源代码时的方式。我觉得描述我所追求的东西的最佳方式是以单元测试的形式。我希望存在一种通用技术可以使以下所有断言都通过,而不是尝试对特殊C#名称的特殊情况进行硬编码。TypeGetFriendlyType(stringtypeName){...??...}voidTest(){//usingfluentassertionsGetFriendlyType("bool").Should().Be(typeof(bool));GetFriendlyType("int").Sho

c# - 如何正确反射(reflect)基本接口(interface)方法

我通过反射调查了2个接口(interface)和2个类:家长IChild-派生自IParent家长子级-派生自父级对我来说奇怪的是,当我通过对IChild类型的反射进行查看时,我没有找到IParent方法。应用于Child类型的相同代码按预期工作-反射显示Parent方法。interfaceIParent{voidParentMethod();}interfaceIChild:IParent{voidChildMethod();}classParent{publicvoidParentMethod(){}}classChild:Parent{publicvoidChildMethod(

c# - 为什么我们必须使用typeof,而不是仅仅使用类型?

尝试将类型分配给类型为System.Type的属性时,为什么我们不能这样做?foo.NetType=bool;编译器产生这个警告:"Expressionexpected."解决它的方法是这样做:foo.NetType=typeof(bool);我的问题是为什么我们不能使用第一种方法?编译器不够聪明,无法弄清楚我们在这里要完成的任务吗?为什么我们必须采用第二种方法(typeof)? 最佳答案 好问题——就语言设计中的一个有趣问题而言。对于本网站来说,这可能不是一个理想的问题,因为它与具体的实际代码无关。设计一种语言可以将类型名称用作表

c++-cli - C++/CLI-问题: Is there an equivalent to the C# "is" keyword or do I have to use reflection?

我在MSDN的某个地方读到过,与C#的“is”关键字等效的是dynamic_cast,但这并不完全等效:它不适用于值类型或泛型参数。例如在C#中我可以写:voidMyGenericFunction(){objectx=...if(xisT)...;}如果我尝试“等效的”C++/CLI:genericvoidMyGenericFunction(){objectx=...if(dynamic_cast(x))...;}我收到编译器错误“errorC2682:cannotuse'dynamic_cast'toconvertfrom'System::Object^'to'T'”。我唯一能想到的

c# - "Inspecting the state of an object in the debuggee of type System.Reflection.MethodBase is not supported in this context"

我不知道这个错误是什么意思。我使用的是VisualStudioforMac7.5.0社区版。我在带有ASP.NETCore的EntityFramework中使用延迟加载。publicpartialclassAdminUser{publicAdminUser(){RoleAssign=newHashSet();}publicGuidUserId{get;set;}publicstringFirstName{get;set;}publicstringLastName{get;set;}publicstringEmail{get;set;}publicstringUserName{get;s