none
Why I cannot start EXE with DynamicAssembly DLL? RRS feed

  • Question

  • I have a problem ...

    I developed proxy class for creation dynamic DLL on runtime and use it in another project by adding in "references" in my project. I write: VC 2010, .NET 4.0, C#.

    public DynamicProxy(
            string nameModule,
            System.Reflection.Emit.AssemblyBuilderAccess accessModule,
            string assemblyFileName = null,
            IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> customAttributes = null)
    {
        try
        {
            // module name
            m_AssemblyFileName = assemblyFileName;
            // try doing something
            AppDomain domain = AppDomain.CurrentDomain;
            // create assembly
            m_MainBuilder = (customAttributes != null) ?
                domain.DefineDynamicAssembly(
                new System.Reflection.AssemblyName(nameModule),
                accessModule,
                customAttributes.ToArray()) :
                domain.DefineDynamicAssembly(
                new System.Reflection.AssemblyName(nameModule),
                accessModule);
            // create module
            m_MainModule = (!string.IsNullOrEmpty(m_AssemblyFileName)) ?
                m_MainBuilder.DefineDynamicModule(m_MainBuilder.GetName().Name, m_AssemblyFileName, true) :
                m_MainBuilder.DefineDynamicModule(m_MainBuilder.GetName().Name, true);
            // create properties storage
            Properties = new Dictionary<string, PropertyTypeInfo>();
        }
            catch (Exception ex)
            {
                throw (new Exception(ex.Message));
            }
    }
    
    public Type CreateInstance(string className, System.Reflection.TypeAttributes typeAccess)
    {
        Type result = null;
        try
        {
            // create class
            System.Reflection.Emit.TypeBuilder typeBuilder = m_MainModule.DefineType("System.Spyrytus.DynamicBuilder." + className, typeAccess);
            // create constructor
            System.Reflection.Emit.ConstructorBuilder cBuilder = typeBuilder.DefineConstructor(System.Reflection.MethodAttributes.Public, System.Reflection.CallingConventions.Standard, Type.EmptyTypes);
            // create event
            System.Reflection.Emit.FieldBuilder eventField =
                typeBuilder.DefineField("PropertyChanged",
                (!UsePropertyChangedEventHandler) ? typeof(PropertyChangedValueEventHandler) : typeof(System.ComponentModel.PropertyChangedEventHandler),
                System.Reflection.FieldAttributes.Private);
            // create event :D
            System.Reflection.Emit.EventBuilder eventBuilder = typeBuilder.DefineEvent(
                "PropertyChanged",
                System.Reflection.EventAttributes.None,
    typeof(System.ComponentModel.PropertyChangedEventHandler));
            // add event handler
            eventBuilder.SetAddOnMethod(CreateAddRemoveMethodsEvent(typeBuilder, eventField, true));
            // remove handler
            eventBuilder.SetRemoveOnMethod(CreateAddRemoveMethodsEvent(typeBuilder, eventField, false));
            // handler
            System.Reflection.Emit.MethodBuilder raise = CreateRaisePropertyChanged(typeBuilder, eventField);
            eventBuilder.SetRaiseMethod(raise);
    
            // create property
            List<System.Reflection.Emit.PropertyBuilder> fields = CreatePropery(ref typeBuilder, cBuilder.GetILGenerator(), raise);
    
            System.Reflection.Emit.MethodBuilder toStrMethod = typeBuilder.DefineMethod(
                "ToString",
                System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.Virtual | System.Reflection.MethodAttributes.HideBySig | System.Reflection.MethodAttributes.ReuseSlot,
                typeof(string),
                Type.EmptyTypes);
            typeBuilder.DefineMethodOverride(toStrMethod, typeof(object).GetMethod("ToString"));
            System.Reflection.Emit.ILGenerator toStr = toStrMethod.GetILGenerator();
            // create StringBuilder in string
            System.Reflection.Emit.LocalBuilder retValue = toStr.DeclareLocal(typeof(StringBuilder));
            toStr.Emit(System.Reflection.Emit.OpCodes.Newobj, retValue.LocalType.GetConstructor(Type.EmptyTypes));
            toStr.Emit(System.Reflection.Emit.OpCodes.Stloc_S, retValue);
            System.Reflection.Emit.Label exception = toStr.BeginExceptionBlock();
            // read all property
            foreach (System.Reflection.Emit.PropertyBuilder piItem in fields)
            {
                // local variable
                System.Reflection.Emit.LocalBuilder local = toStr.DeclareLocal(piItem.PropertyType);
                // read value
                toStr.Emit(System.Reflection.Emit.OpCodes.Ldarg_0);
                toStr.Emit(System.Reflection.Emit.OpCodes.Call, piItem.GetGetMethod());
                toStr.Emit(System.Reflection.Emit.OpCodes.Stloc_S, local);
                // create Format
                toStr.Emit(System.Reflection.Emit.OpCodes.Ldloc_S, retValue);
                toStr.Emit(System.Reflection.Emit.OpCodes.Ldstr, piItem.Name + ": {0}\r\n");
                toStr.Emit(System.Reflection.Emit.OpCodes.Ldloc_S, local);
                toStr.Emit(System.Reflection.Emit.OpCodes.Box, piItem.PropertyType);
                toStr.Emit(System.Reflection.Emit.OpCodes.Callvirt, retValue.LocalType.GetMethod("AppendFormat", new Type[] { typeof(string), piItem.PropertyType }));
                toStr.Emit(System.Reflection.Emit.OpCodes.Pop);
            }
            toStr.BeginCatchBlock(typeof(Exception));
            toStr.EndExceptionBlock();
            // exit from method
            toStr.Emit(System.Reflection.Emit.OpCodes.Ldloc_S, retValue);
            toStr.Emit(System.Reflection.Emit.OpCodes.Callvirt, retValue.LocalType.GetMethod("ToString", Type.EmptyTypes));
            toStr.Emit(System.Reflection.Emit.OpCodes.Ret);
    
            // create type
            result = typeBuilder.CreateType();
        }
        catch (Exception ex)
        {
            throw (new Exception(ex.Message));
        }
        return result;
    }

    Create dynamic object:

    using (Spyrytus.Windows.MSSQL.DynamicProxy proxy = new Spyrytus.Windows.MSSQL.DynamicProxy("module", System.Reflection.Emit.AssemblyBuilderAccess.Save, "object.dll", attrs))
    {
        proxy.CreateInstance("Foo", System.Reflection.TypeAttributes.Public);
        proxy.Save();
    }

    Add this DLL in refrences by right-click mouse, and try use it:

    System.Spyrytus.DynamicBuilder.Foo t = new System.Spyrytus.DynamicBuilder.Foo()

    And error:

    An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll Additional information: Could not load file or assembly 'module, Version=6.11.395.610, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Could not found special file.

    

    What is it means ?

    P.S. This code from reflector:

    public class Foo
    {
        // Fields
        private int m_id = 0;
        private PropertyChangedEventHandler PropertyChanged = null;
    
        // Events
        public event PropertyChangedEventHandler PropertyChanged
        {
            [MethodImpl(MethodImplOptions.Synchronized), SecuritySafeCritical] add
            {
                this.PropertyChanged = (PropertyChangedEventHandler) Delegate.Combine(this.PropertyChanged, handler1);
            }
            [MethodImpl(MethodImplOptions.Synchronized), SecuritySafeCritical] remove
            {
                this.PropertyChanged = (PropertyChangedEventHandler) Delegate.Remove(this.PropertyChanged, handler1);
            }
            raise
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(text1));
                }
            }
        }
    
        public gopa()
        {
            this.m_id = 0;
            this.PropertyChanged = null;
        }
    
     
        // Methods
        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                int id = this.id;
                builder.AppendFormat("id: {0}\r\n", id);
            }
            catch (Exception)
            {
            }
            return builder.ToString();
        }
    
        // Properties
        public int id
        {
            get
            {
                return this.m_id;
            }
            set
            {
                this.m_id = value;
                this.raise_PropertyChanged("id");
            }
        }
    }
    
     
    Collapse Methods
     

    Thanks In Advance.

    ---

    Regards: Eugene.


    Wednesday, June 11, 2014 11:12 AM

Answers

  • Hi Eugene,

    We can use the assembly binding log viewer to display details for assembly binds. Please refer to the following link. http://msdn.microsoft.com/en-us/library/e74a18c4%28v=vs.110%29.aspx.

    Please post the whole test project here to help reproduce this problem. I cannot guess the definition of PropertyTypeInfo class. See you later.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 12, 2014 3:22 AM
    Moderator