C# CodeDom:自定义 class 作为字符串代码中的参数
C# CodeDom: Custom class as parameter in string code
抱歉,我是使用 CodeDom 编程的新手,我遇到了无法解决的问题。正在考虑你们中的一些人是否知道解决方案。
我在 class 文件中有以下代码:
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;
}
我想输入 data(自定义创建的 class,其中包含另一个自定义 class 的数组)变量作为参数在 DoStuff 函数中,我一直有错误。有办法解决这个问题吗?
多处错误,改成这样:
string expression = @"using System;
namespace MyNamespace {
public class MyClass {
public static int DoStuff(CustomClass myCustomClass) {
return myCustomClass.X + myCustomClass.Y;
}
}
public class CustomClass
{
public int X;
public int Y;
public CustomClass(int x, int y) { X = x; Y = y; }
}
}";
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters assemblies = new CompilerParameters(new string[] { "System.dll" });
CompilerResults results = provider.CompileAssemblyFromSource(assemblies, expression);
Type temporaryClass = results.CompiledAssembly.GetType("MyNamespace.MyClass");
Type parClass = results.CompiledAssembly.GetType("MyNamespace.CustomClass");
MethodInfo temporaryFunction = temporaryClass.GetMethod("DoStuff");
object mc = parClass.GetConstructor(new Type[] { typeof(int), typeof(int) }).Invoke(new object[]{1,2});
object result = temporaryFunction.Invoke(null, new object[] { mc });
抱歉,我是使用 CodeDom 编程的新手,我遇到了无法解决的问题。正在考虑你们中的一些人是否知道解决方案。
我在 class 文件中有以下代码:
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;
}
我想输入 data(自定义创建的 class,其中包含另一个自定义 class 的数组)变量作为参数在 DoStuff 函数中,我一直有错误。有办法解决这个问题吗?
多处错误,改成这样:
string expression = @"using System;
namespace MyNamespace {
public class MyClass {
public static int DoStuff(CustomClass myCustomClass) {
return myCustomClass.X + myCustomClass.Y;
}
}
public class CustomClass
{
public int X;
public int Y;
public CustomClass(int x, int y) { X = x; Y = y; }
}
}";
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters assemblies = new CompilerParameters(new string[] { "System.dll" });
CompilerResults results = provider.CompileAssemblyFromSource(assemblies, expression);
Type temporaryClass = results.CompiledAssembly.GetType("MyNamespace.MyClass");
Type parClass = results.CompiledAssembly.GetType("MyNamespace.CustomClass");
MethodInfo temporaryFunction = temporaryClass.GetMethod("DoStuff");
object mc = parClass.GetConstructor(new Type[] { typeof(int), typeof(int) }).Invoke(new object[]{1,2});
object result = temporaryFunction.Invoke(null, new object[] { mc });