C# CSharpCodeProvider中的属性
我尝试编译一些简单的代码(一个c#类),它使用自己的属性。 如果类的my属性不使用自定义属性,CSharpCodeCompiler不会引发错误 这是我的字符串:C# CSharpCodeProvider中的属性,c#,compilation,csharpcodeprovider,C#,Compilation,Csharpcodeprovider,我尝试编译一些简单的代码(一个c#类),它使用自己的属性。 如果类的my属性不使用自定义属性,CSharpCodeCompiler不会引发错误 这是我的字符串: string _string = @" using System; using System.Collections.Generic; namespace Generator { public class ExampleClass { public int id { get; set; } }
string _string = @"
using System;
using System.Collections.Generic;
namespace Generator
{
public class ExampleClass
{
public int id { get; set; }
}
}"
var csc = new CSharpCodeProvider();
var cc = csc.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("mscorlib.dll");
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.ComponentModel.DataAnnotations.dll");
cp.ReferencedAssemblies.Add("System.ComponentModel.dll");
cp.ReferencedAssemblies.Add("System.Runtime.InteropServices.dll");
cp.ReferencedAssemblies.Add(typeof(GUID).Assembly.Location);
CompilerResults results = csc.CompileAssemblyFromSource(cp, _string);
System.Reflection.Assembly _assembly = results.CompiledAssembly;
Type[] _types = _assembly.GetTypes();
Type eType = _assembly.GetType(namespaceAndClass);
它是用上述代码完美编译的。
但是如果我使用自定义属性,它将抛出一个错误
我的属性类:
using System;
namespace Generator
{
[AttributeUsage(AttributeTargets.All)]
class GUID : Attribute
{
private string guid { get; set; }
public GUID()
{
this.guid = Guid.NewGuid().ToString();
}
public GUID(string value)
{
this.guid = value;
}
public string getGuid()
{
return guid;
}
}
}
我需要使用自定义属性编译字符串
...
[GUID()]
公共int id{get;set;}
当我尝试此操作时,会出现以下错误:
引发异常:mscorlib.dll中的System.IO.FileNotFoundException请提供完整的错误消息和堆栈跟踪。仅此而已。。。完整的错误消息只有一行。在哪里使用自定义属性?