动态调用方法和类名

编程入门 行业动态 更新时间:2024-10-26 22:26:29
本文介绍了动态调用方法和类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在某些情况下,我必须从类名中调用方法名。

I have some cases where I have to call method names from class names.

string scenario1 = "MockScenario1"; string scenario2 = "MockScenario2"; MockScenario1.GetInfo(); MockScenario2.GetInfo();

如何动态地使用字符串在此处调用方法名称

How can I dynamically use the strings to call method name here like

scenario1.GetInfo() scenario2.GetInfo()

我试图通过字符串找出所有选项并控制空间以找到相关的选项。有什么建议吗?

I tried to find out all options by string and control space to find related options. Any suggestions?

我在下面尝试了并尝试获取动态生成的类名

I am tried the below and trying to Get class name generated dynamically

下面的代码生成了动态方法名称

The below code generated method name dynamically

string methodName = "hello"; //Get the method information using the method info class MethodInfo mi = this.GetType().GetMethod(methodName); //Invoke the method // (null- no parameter for the method call // or you can pass the array of parameters...) mi.Invoke(this, null);

更清晰的场景:我试图将类名作为参数发送MockScenario1和MockScenario2是类名。

More clear scenario: I am trying to send class name as parameter MockScenario1 and MockScenario2 are class names.

string scenarioCats = "MockScenario1"; string scenarioDogs = "MockScenario2"; GetResult(scenarioCats); GetResult(scenarioDogs); public static void GetCatsResult(string scenarioCats){ scenarioCats obj = new scenarioCats(); obj.GetInfo(); } public static void GetDogsResult(string scenarioDogs){ scenarioDogs obj = new scenarioDogs(); obj.GetInfo(); }

推荐答案

如何创建实例从其字符串表示形式中得到的类型:

How to create an instance of a type from its string representation:

string scenario1 = "TheNamespace.MockScenario1"; Type theType = this.GetType().Assembly.GetType(scenario1); var theInstance = (MockScenario1)Activator.CreateInstance(theType); theInstance.GetInfo();

如果您的类实现了公共接口,例如 IGetInfoAware ,然后可以编写一个更通用的加载器:

It will be better if your classes implement a common interface, for example IGetInfoAware, and then you could write a more generic loader:

var theInstance = (IGetInfoAware)Activator.CreateInstance(theType);

注意:您需要提供方案1和方案2的完整类名

Note: you need to provide the full class name for scenario1 and scenario2

请参见 Activator.CreateInstance

编辑:

为@ Georg指出,如果未在上下文对象的程序集中声明该类型,则必须首先获取托管该类型的程序集:

As @Georg pointed out, if the type is not declared in the assembly of the context objects, then it is necessary first to get the assembly where the type is hosted:

var theAssembly = ( from Assembly assembly in AppDomain.CurrentDomain.GetAssemblies() where (assembly.FullName == "TheNamespace.AssemblyName") select assembly ) .FirstOrDefault(); if ( theAssembly!= null ){ Type theType = theAssembly.GetType(scenario1); var theInstance = (IGetInfoAware)Activator.CreateInstance(theType); theInstance.GetInfo(); }

如果由于某种原因您不知道程序集名称,则该类型可以可以像下面这样解决:

If for some reason the assembly name is unknown to you, then the type could be resolved like the following:

public Type GetTypeFromString(String typeName) { foreach (Assembly theAssembly in AppDomain.CurrentDomain.GetAssemblies()) { Type theType = theAssembly.GetType(typeName); if (theType != null) { return theType; } } return null; }

更多推荐

动态调用方法和类名

本文发布于:2023-11-11 23:51:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579893.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!