0

I try to compile some simple code (a c# class), which is using own Attribute. The CSharpCodeCompiler doesn't throw an error if my properties of class do not use the custom Attribute.

this is my String:

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);

It is perfectly compiled with the above code. But if I use my custom Attribute it will throw an error.

My attribute class:

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;
        }
    }
}

I need the string to be compiled with my custom Attributes ... [GUID()] public int id { get; set; }

When I try this, I get this error: Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll

0 Answers0