如何执行具有接口的C#文件
我有一个继承接口的文本(code.txt)文件如何执行具有接口的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
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中的代码,它应该可以工作。