0

Cシャープの反射について質問があります..これは私の質問です

1) 異なるアクセサー (private、public、protected) を持つ異なるフィールドを持つクラス MyClass と、異なる引数セットと戻り値を持つメソッドを定義します。

2) メソッドを含む MyTestClass を定義します。これは、次のことを行います: 特定のクラス名のメソッド名を出力します。メソッドには文字列引数が含まれ、クラス名は文字列値です。クラスのいくつかのメソッドを呼び出し、メソッドに引数を入れます。引数はテキストファイルから読み取る必要があります(クラスの名前とメソッドの名前はメソッドの引数です

私は質問に答えましたが、問題があります。テキストファイル内でプログラムを実行すると、テキストファイルから読み取ることができません。二重の数字と整数がありますが、コンソールに表示されません。これは私のコードです

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.IO;

class MyClass
{
    private int i;
    public double d;
    private string s;
    public bool b;
    public MyClass()
    {
        i = 1;
        d = 0.1;
        s = "1";
        b = true;
    }
    public void Method0()
    {
        Console.WriteLine("Method with no arguments, no return value.");
    }
    private int Method1(int arg0)
    {
        Console.WriteLine("The method returns int, int gets.");
        return arg0;
    }
    private double Method2(int arg0, double arg1)
    {
        Console.WriteLine("Method returns a double, taking int and double.");
        return arg1 * arg0;
    }
    public bool Method3(string arg0)
    {
        Console.WriteLine("Method returns a bool, accepts string");
        return arg0.Length>10;
    }
    public bool Method3(string arg0,string arg1)
    {
        Console.WriteLine("The method takes two arguments string.");
        return arg0 == arg1;
    }
    public static char Method4(string arg0)
    {
        Console.WriteLine("Method returns a char, accepts string. .");
        return arg0[1];
    }
    public void Method5(int arg0, double arg1)
    {
        Console.WriteLine("arg1 = {0} arg2 = {1}.",arg0,arg1);
    }
}

class MyTestClass
{
    public static string[] GetMethodsWithStrParams(string className)
    {
        var t = Type.GetType(className);
        List<string> res = new List<string>();
        foreach (var method in t.GetMethods())
        {
            foreach (var param in method.GetParameters())
            {
                if (param.ParameterType == typeof(string))
                {
                    res.Add(method.Name);
                    break;
                }
            }
        }
        return res.ToArray();
    }
    public static void InvokeMethod(string className, string methodName, string fileName)
    {
        var t = Type.GetType(className);
        using (StreamReader f = new StreamReader("params.txt"))
        {
            t.GetMethod(methodName).Invoke(t.GetConstructor(Type.EmptyTypes).Invoke(new object[] { }),
                                           new object[] { f.ReadLine() });
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        string name = "MyClass";

        foreach (var x in MyTestClass.GetMethodsWithStrParams(name))
        {
            Console.WriteLine(x);
        }

        MyTestClass.InvokeMethod("MyClass", "Method4", "params.txt");

        Console.ReadKey(true);
    }
}

これは、プログラムを実行したときの出力です

method3
method3
method4
Method returns a char, accepts string. .

しかし、params.txt内には

10 1.5
4

2 に答える 2