C# 在c sharp中使用反射和流读取器
我有一个关于c语言的思考问题这是我的问题 1) 使用具有不同访问器(private、public、protected)的不同字段以及具有不同参数集和返回类型的方法定义类MyClass 2) 定义包含方法的MyTestClass,它执行以下操作:打印指定类名的方法名,其中方法包含字符串参数,类名是字符串值。调用类的某个方法,并将参数放入方法,参数应从文本文件中读取(类的名称和方法的名称是方法的参数) 我已经回答了这个问题,但是我有一个问题,当我在文本文件中运行程序时,我无法读取文本文件。文本文件中有一个双精度数字和一个整数,但它没有显示在控制台上 这是我的密码C# 在c sharp中使用反射和流读取器,c#,C#,我有一个关于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);
}
}
但是在params.txt中有
10 1.5
一些建议:
将反射部分分为两部分:1)对象的构造;2) 方法的调用。这使代码更加清晰,并且您可以在调用下一个方法时重用该对象
f.ReadLine()
返回单个字符串。它不会自动将其拆分为单独的参数。新对象[]{f.ReadLine()}
提供了一个具有单个字符串值的对象数组。您需要在一些分隔符上将该行拆分为单独的值。然后需要将这些单独的字符串转换为方法所需的参数类型。我在visual Studio中运行了您的代码,唯一的问题是文件名。其余的似乎在原则上起作用。文件params.txt肯定在可执行文件夹中吗?如果没有,则需要提供完整路径
MyTestClass.InvokeMethod("MyClass", "Method4", "c:\\params.txt");
我还有一个问题是?MyClass是否存在于命名空间中?如果是这样,则需要使用命名空间返回类型对象:
MyTestClass.InvokeMethod("MyNamespace.MyClass", "Method4", "c:\\params.txt");
我还注意到,尽管方法InvokeMethod接受filename作为参数,但它有一个硬编码的文件名。因此,它会忽略调用代码(在main方法中)传递的文件名。您需要向我们显示输出内容。就目前而言,很难理解这个问题。好的,让我编辑它,并向您显示输出您的代码工作正常,如果您编写
Console.WriteLine(arg0)在Method4
中,您将在控制台中接收10 1.5
。请告诉我们您希望输出的内容。@DmitryDovgopoly谢谢,我想那是丢失的部分
MyTestClass.InvokeMethod("MyNamespace.MyClass", "Method4", "c:\\params.txt");