Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文件路径获取类名_C#_Class_Path - Fatal编程技术网

C# 从文件路径获取类名

C# 从文件路径获取类名,c#,class,path,C#,Class,Path,我有一个系统,可以读取文件夹并将路径保存到数组列表中。此文件夹包含一组类文件“.cs”所有这些类都实现了一个名为BaseScript的接口 我正在尝试查找类名,以便调用类方法,但您应该能够定期将类文件添加到此文件夹,因此我无法硬编码要创建的对象 e、 g.我有一个名为“Manufacturing.cs”的文件: 它实现了“BaseScript”: 我需要能够使用路径:“\Scripts\Manufacturing.cs”来调用 BaseScript[] s = {new "Manufacturi

我有一个系统,可以读取文件夹并将路径保存到数组列表中。此文件夹包含一组类文件“.cs”所有这些类都实现了一个名为BaseScript的接口

我正在尝试查找类名,以便调用类方法,但您应该能够定期将类文件添加到此文件夹,因此我无法硬编码要创建的对象

e、 g.我有一个名为“Manufacturing.cs”的文件:

它实现了“BaseScript”:

我需要能够使用路径:“\Scripts\Manufacturing.cs”来调用

BaseScript[] s = {new "Manufacturing class name"}
我知道一定有什么迂回的方法。
我怎样才能找到类名,然后从该类创建实例?

编辑-我错了,尽管出于某种原因您有DLL而没有cs文件

正如其他人所说,你没有正确地解释这个场景。但据我所知,您可能在这里实现了一种插件类型的模式。 为了在以后可能添加的C#类上调用方法,您需要遍历文件夹,获取*.cs文件,编译它们,然后使用反射,加载程序集并创建一个已知存在的类的实例(您应该事先知道该类的完全限定名)。最后,您应该找到要调用的方法,然后使用反射调用它。以下是一些让您开始学习的代码:

Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.CompilerParameters parameters = new System.CodeDom.Compiler.CompilerParameters();
parameters.GenerateInMemory = true;
//You should add all referenced assembiles needed for the cs file here
//parameters.ReferencedAssemblies.Add();

var files = new System.IO.DirectoryInfo("Scripts").GetFiles("*.cs");
foreach (System.IO.FileInfo file in files)
{
    System.CodeDom.Compiler.CompilerResults result = 
        provider.CompileAssemblyFromSource(parameters, new[] { System.IO.File.ReadAllText(file.FullName) });
    object instance = result.CompiledAssembly.CreateInstance(file.Name);
    if (instance is BaseScript)
    {
        //Do processing on (BaseScript)instance
    }
}

如果类位于同一程序集中,则可以使用创建对象的实例。我没有在我这边尝试你的代码。所以铸造可能不起作用。再次检查此代码:

BaseScript s = (BaseScipt)Activator.CreateInstance(null, "Namespace.TestClass");

不知道你想做什么。是否要动态编译这些CS文件,确定它们是否继承/派生自特定的类/接口,如果是,则实例化它们并调用方法?我还建议您至少写一个问句(通常包括使用问号?:)这很容易混淆,您希望能够读取类名,然后创建它的实例吗?或者只是想要一个带有类名的字符串(基于离线的
BaseScript[]s={new“Manufacturing class name”}
),我愿意打赌有一种方法可以满足您的需求,但是我愿意赌更多的钱,通过提出一个特定的问题,您可以得到一个特定且有用的答案。对不起,问题已更新,tnw一语中的。我需要找到类名,然后创建该类的实例。编辑:很抱歉这个糟糕的问题,我现在应该更清楚了。@MattHirdler仍然不清楚,为什么要从源文件中获取类名?你想按照Eric的要求编译它们吗?
Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.CompilerParameters parameters = new System.CodeDom.Compiler.CompilerParameters();
parameters.GenerateInMemory = true;
//You should add all referenced assembiles needed for the cs file here
//parameters.ReferencedAssemblies.Add();

var files = new System.IO.DirectoryInfo("Scripts").GetFiles("*.cs");
foreach (System.IO.FileInfo file in files)
{
    System.CodeDom.Compiler.CompilerResults result = 
        provider.CompileAssemblyFromSource(parameters, new[] { System.IO.File.ReadAllText(file.FullName) });
    object instance = result.CompiledAssembly.CreateInstance(file.Name);
    if (instance is BaseScript)
    {
        //Do processing on (BaseScript)instance
    }
}
BaseScript s = (BaseScipt)Activator.CreateInstance(null, "Namespace.TestClass");