對不起,我是使用CodeDom進行編程的新手,我遇到了這個我無法解決的問題。想着如果你們中的一些人知道解決方案。C#CodeDom:在字符串代碼中作爲參數的自定義類
我有一個類文件中的下面的代碼:
public class AnotherCustomClass {
public string Employer { get; set; }
public DateTime? DateOfHire { get; set; }
}
public class CustomClass {
public int X { get; set; }
public int Y { get; set; }
public AnotherCustomClass[] YetAnother { get; set; }
}
public void DoSomething()
{
string expression = @"using System;
namespace MyNamespace {
public class MyClass {
public static int DoStuff(CustomClass myCustomClass) {
return myCustomClass.X + myCustomClass.Y;
}
}
}";
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters assemblies = new CompilerParameters(new[] { "System.Core.dll" });
CompilerResults results = provider.CompileAssemblyFromSource(assemblies, expression);
Type temporaryClass = results.CompiledAssembly.GetType("MyNamespace.MyClass");
MethodInfo temporaryFunction = temporaryClass.GetMethod("DoStuff");
CustomClass data = new CustomClass() { X = 1, Y = 2 };
object result = temporaryFunction.Invoke(null, new object[] { data });
return result;
}
我想輸入數據(自定義創建的類,其具有內部另一自定義類的數組)變量作爲參數在DoStuff功能,我一直有錯誤。有沒有辦法解決這個問題?
對不起,你的問題是不明確的。請說明你想要用它做什麼。 –
好,所以你想編譯這個?這就是問題所在?添加一些額外的信息。例如'CustomClass'的位置以及你被拖拽的位置? –
[是否可以動態編譯和執行C#代碼片段?](http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp -code-fragments) –