Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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# - Fatal编程技术网

如何执行具有接口的C#文件

如何执行具有接口的C#文件,c#,C#,我有一个继承接口的文本(code.txt)文件 public class TablePrinting : ITable { public void Table() { Console.WriteLine("This Is Table Method!!!!!"); Console.WriteLine("Dynamic Binding Is Successful !!!"); } } 我还写了一个代码VS

我有一个继承接口的文本(code.txt)文件

public class TablePrinting : ITable 
{

    public void Table()
        {
            Console.WriteLine("This Is Table Method!!!!!");
            Console.WriteLine("Dynamic Binding Is Successful !!!");

        }
}
我还写了一个代码VS 2013

namespace DemoTablePrinting
{

    class Program
    {
        interface ITable
    {
        void Table();
    } 
        static void Main(string[] args)
        { 


            string Content =File.ReadAllText(@"C:\Users\Asif\Desktop\code.txt");
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters parameter = new CompilerParameters();
            parameter.ReferencedAssemblies.Add("System.dll");
            parameter.GenerateInMemory = false;
            parameter.GenerateExecutable = true;

            CompilerResults result =provider.CompileAssemblyFromSource(parameter, Content);
            if (result.Errors.Count > 0)
            {

                Console.WriteLine("Errors building\n\n {0} into {1} ",Content, result.PathToAssembly);
                foreach (CompilerError ce in result.Errors)
                {
                    Console.WriteLine("  {0}", ce.ToString());
                    Console.WriteLine();
                }
            }
            else
            {
                Console.WriteLine("Source\n\r {0} built successfully.",Content);
            }
            Assembly assembly = result.CompiledAssembly;
            Type type = assembly.GetType("hello.TablePrinting");

            Object obj = Activator.CreateInstance(type);
        }
    }
}
但它给出了一个编译错误,并且找不到可执行文件
现在我如何使用interface调用文本文件的所有方法

您已经在
程序中定义了接口
ITable
。如果执行此操作,则需要将其称为
Program.ITable
,或在定义“TablePrinting”的文件中包含
Using Program

public class TablePrinting : ITable 
{

    public void Table()
        {
            Console.WriteLine("This Is Table Method!!!!!");
            Console.WriteLine("Dynamic Binding Is Successful !!!");

        }
}
如果ITable不是特定于
程序
,则应将接口移到类之外


您可以嵌套类,但通常仅当您希望暗示内部对象链接到外部对象时才进行嵌套。

您的code.txt文件不会编译,因为ITable未在using语句中引用,而是在程序中声明的。我建议将接口从那里移到代码中。TXT是一个小程序,如果我不使用接口,那么程序将成功运行。但是如果我想在我们的程序中使用接口,那么这是不可能的?@asifhusinjafri在您的项目中,将code.txt中的代码添加为.cs文件,然后尝试编译它-根据我之前的建议修复错误,然后将该代码作为code.txt中的代码,它应该可以工作。