草庐IT

Reflection

全部标签

c# - 如何从作为对象获取的 IList<> 中获取项目计数?

在一个方法中,我得到一个object.在某些情况下,这object可以是IList“某事”(我无法控制这个“某事”)。我正在尝试:识别这个对象是一个IList(某事)投object进入“IList”以获得Count现在,我陷入困境并寻找想法。 最佳答案 您可以使用is检查您的object是否实现了IList。然后您可以将您的对象转换为IList以获取计数。objectmyObject=newList();//checkifmyObjectimplementsIListif(myObjectisIList){intlistCount=

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

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

c# - 反射和通用类型

我正在为一个类构造函数编写一些代码,它循环遍历该类的所有属性并调用一个通用静态方法,该方法使用来自外部API的数据填充我的类。所以我把它作为一个示例类:publicclassMyClass{publicstringProperty1{get;set;}publicintProperty2{get;set;}publicboolProperty3{get;set;}publicstaticTDoStuff(stringname){//getthedataforthepropertyfromtheexternalAPI//orifthere'saproblemreturn'default(

c# - 满足开放/封闭原则的工厂模式?

我有以下具体的Animal产品:Dog和Cat。我正在使用parameterizedFactorymethod创造所述产品。根据传递给Factory方法的AnimalInfo参数,将创建一个具体的产品。映射逻辑放在工厂方法中。这是我的代码:publicabstractclassAnimalInfo{publicabstractStringSound{get;}}publicclassDogInfo:AnimalInfo{publicoverridestringSound{get{return"Bark";}}}publicclassCatInfo:AnimalInfo{publicov

c# - 如何使用反射获得重载的私有(private)/ protected 方法

usingSystem;usingSystem.Reflection;namespaceReflection{classTest{protectedvoidmethodname(inti){Console.WriteLine(("intheworldofthereflection-onlyi"));Console.Read();}protectedvoidmethodname(inti,intj){Console.WriteLine(("intheworldofthereflectioni,j"));Console.Read();}}classProgram{staticvoidMai

c# - C#中使用反射制作动态LINQ语句

如果我有一个像这样的LINQ语句x=Table.SingleOrDefault(o=>o.id==1).o.name;如何使用反射将传入的变量替换为“id”和“name”?当我尝试时,我不断收到对象引用未设置为对象错误的实例。我的尝试是这样的x=(string)Table.SingleOrDefault(o=>(int?)o.GetType().GetProperty(idString).GetValue(o,null)==1).GetType().GetField(nameString).GetValue(x);任何帮助都会很棒。谢谢。 最佳答案

c# - 如何通过反射获取可空类型的值

我需要使用反射来检索NullableTypeofDateTime的属性值我该怎么做?当我尝试propertyInfo.GetValue(object,null)时,它不起作用。谢谢我的代码:varpropertyInfos=myClass.GetType().GetProperties();foreach(PropertyInfopropertyInfoinpropertyInfos){objectpropertyValue=propertyInfo.GetValue(myClass,null);}对于可为null的类型,propertyValue结果始终为null

c# - C#中的函数指针

我想在某些方面(或两者)Delegate或MethodInfo符合这个标题。但是,两者都没有提供我正在寻找的语法优美性。所以,简而言之,有什么方法可以写出以下内容:FunctionPointerfoo=//whatever,createthefunctionpointerusingmechanismsfoo();我不能使用实体委托(delegate)(即,使用delegate关键字来声明委托(delegate)类型),因为在运行时之前无法知道确切的参数列表。作为引用,这是我目前在LINQPad中玩弄的内容,其中B将(主要)是用户生成的代码,Main也是如此,因此为了美观我的用户,我正在

c# - GetExecutingAssembly() 用于不同程序集中的派生类

我有一个插件架构,其中在主应用程序中定义了一个抽象基类。它使用反射来加载具有基类的派生实现的程序集。我想获取包含派生对象的程序集的版本信息。这是我的基类:namespaceMy.AppNameSpace{publicabstractclassBaseClass{publicVersionVersion{get{Assemblyassembly=Assembly.GetExecutingAssembly();FileVersionInfofvi=FileVersionInfo.GetVersionInfo(assembly.Location);stringversion=fvi.File

c# - 如何在 C# 中从字符串创建实例?

我正在从XML中读取信息,其中包含我需要实例化的对象类型及其构造函数参数。对象类型实际上在另一个项目中,在同级命名空间中。(我需要在Company.Project1类中创建一个Company.Project2.Type。)我找到了thisquestion,但它不处理构造函数参数或它在另一个命名空间中的事实。我该怎么做?编辑:程序集名称和默认命名空间未在项目属性中正确设置。 最佳答案 您需要将完整的类型名称指定为Type.GetType(),包括命名空间,例如“公司.项目2.类型”如果类型不在同一个程序集(或mscorlib)中,您还