Skip to main content
 首页 » 编程设计

c#之使用字符串调用特定的命名空间和类

2024年02月06日25JustinYoung

我目前正在用 C# 编写一个程序,这个程序有一个名为 Classes 的文件夹,许多子文件夹名为 a - f,以及该文件夹中名为 aa - cc 的实际 cs 文件。

a-f 是命名空间 aa-cc 是类。

因此,如果我想从 Classes\b\bb.cs 中调用一个方法,我必须每次都输入

b.bb,methodName(); 

它还会生成两个字符串,这两个字符串将决定我们要执行的内容。

String myType = "b"; 
String myClass = "bb"; 
String toReturn = ""; 

myType 和 myClass 有很多组合,所以我不得不写很多 if 循环

if (myType.equals("b") && myClass.equals("bb") return toReturn = b.bb.myMethod(); 
if (myType.equals("b") && myClass.equals("aa") return toReturn = b.aa.myMethod(); 

这是相当多的输入,我觉得有一种更简单的方法可以做到这一点,因为我已经知道我想访问什么命名空间以及我想调用什么方法。

有什么办法可以做类似的事情

myType.myClass.myMethod() 

其中 myType 是命名空间,myClass 是我希望调用的类。这样可以避免我输入任何可能的组合。

请您参考如下方法:

改进了代表示例。作业:在 GetDelegate 方法中添加哈希以存储委托(delegate)。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Reflection; 
 
using TestConsole.Data; 
 
namespace NamespaceA 
{ 
    public class ClassAB 
    { 
        public void MyMethod() 
        { 
            Console.WriteLine("You invoke NamespaceA.ClassAB.MyMethod"); 
        } 
 
        public static void MyStaticMethod() 
        { 
            Console.WriteLine("You invoke NamespaceA.ClassAB.MyStaticMethod"); 
        } 
    } 
} 
 
namespace TestConsole 
{ 
    public delegate void DoSomethingUseful();   // same as Action delegate 
 
    class Program 
    { 
        /// <summary> 
        /// create delegate 
        /// </summary> 
        /// <param name="namespace"> namespace name </param> 
        /// <param name="class"> class name </param> 
        /// <param name="method"> method name </param> 
        /// <returns></returns> 
        public static DoSomethingUseful GetDelegate(string @namespace, string @class, string method) 
        { 
            // common argument checks 
            if (@namespace == null) throw new ArgumentNullException("namespace"); 
            if (@class == null) throw new ArgumentNullException("class"); 
            if (method == null) throw new ArgumentNullException("method"); 
 
            // find class 
            Type type = Type.GetType(@namespace + "." + @class); 
            if (type == null) throw new Exception("type not found"); 
 
            // find method 
            MethodInfo methodInfo = type.GetMethod(method); 
            if (methodInfo == null) throw new Exception("method not found"); 
 
            // create the delegate 
            return (DoSomethingUseful)Delegate.CreateDelegate(typeof(DoSomethingUseful), methodInfo.IsStatic ? null : Activator.CreateInstance(type), methodInfo); 
        } 
 
        static void Main(string[] args) 
        { 
            // creating delegates 
            DoSomethingUseful methodA = GetDelegate("NamespaceA", "ClassAB", "MyMethod"); 
            DoSomethingUseful methodB = GetDelegate("NamespaceA", "ClassAB", "MyStaticMethod"); 
 
            // usage 
            methodA(); 
            methodB(); 
 
            Console.WriteLine(); 
            Console.WriteLine("Press any key to continue..."); 
            Console.ReadKey(); 
        } 
    } 
}