简体   繁体   中英

MissingMethodException on Activator.CreateInstance

I'm trying to create an instance of a foreign DLL. Unfortunately I get the error message "MissingMethodException". The command Activator.CreateInstance produces this error.
The creation:

Assembly lAssembly = 
    Assembly.Load(File.ReadAllBytes(@"C:\Siemens.Engineering.dll"));
Type lType = lAssembly.GetType("Siemens.Engineering.TiaPortal", true, true);

return (dynamic)Activator.CreateInstance(type: lType, 
            args: new object[] { 
                    Siemens.Engineering.TiaPortalMode.WithoutUserInterface 
                  });

the definition of the dll (abbreviated):

namespace Siemens.Engineering {
    //
    // Zusammenfassung:
    //     TIAPortal.
    [DebuggerNonUserCode]
    [SecuritySafeCritical]
    public sealed class TiaPortal : IApplicationEntryPoint,
                                    IInternalApplicationAccess,
                                    IInternalObjectAccess,
                                    IInternalInstanceAccess,
                                    IInternalBaseAccess,
                                    IEngineeringRoot,
                                    IEngineeringObject,
                                    IEngineeringCompositionOrObject,
                                    IEngineeringInstance, 
                                    IEngineeringServiceProvider,
                                    IServiceProvider,
                                    IEquatable<object>,
                                    IDisposable 
    {
        //
        // Zusammenfassung:
        //     Initializes a new instance of the 
        // Siemens.Engineering.TiaPortal class.
        //
        // Parameter:
        //   tiaPortalMode:
        //     TIA-Portal will start in this mode.
        [SecuritySafeCritical]
        public TiaPortal(TiaPortalMode tiaPortalMode = 
                          TiaPortalMode.WithoutUserInterface);
    }
}

I don't know what I'm doing wrong with this call. Since the constructor has an optional parameter, it should be able to be called with and without parameters. But both variants produce the same error. The assembly (lAssembly), as well as the type (lType), can be loaded correctly.

Any advice?

I found the problem. The addressed DLL is looking for a hard-coded path. If this does not exist, the same cannot be instantiated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM