c# - Call a method from an invoked object -


i create instance of object can use elsewhere in class without going through more reflection if can it.

after creating object (either through activator or constructorinfo, i've read arguments both used activator shorter example) have cast doesn't work.

for example:

server instance;  public void a() {     assembly loaded = assembly.loadfrom("c:\program files\test2\shared.dll");     servertype = loaded.gettype("server");     object obj = activator.createinstance(servertype, new [] { "test" });     instance = (server)obj; }  public void b() {     instance.startserver(); } 

this gives exception:

    system.invalidcastexception: [a]server cannot cast [b]server.      type originates 'shared, version=0.0.0.0, culture=neutral,           publickeytoken=2a032d22a4688508' in context      'loadfrom' @ location 'c:\program files\test1\shared.dll'.      type b originates 'shared, version=0.0.0.0, culture=neutral,     publickeytoken=2a032d22a4688508' in context 'default' @ location .     'c:\program files\test2\shared.dll' 

i understand reason exception thrown, there way cast instance server type want, ie dynamically loaded dll?

on line: instance = (server)obj; need specify fully qualified name of type server.

in assembly run code, probabbly present type server, compiler tries resolve ambiguity, picking local one. need specify, you're going cast server class of test.dll assembly or other specific, like: namespace.type_name


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -