Type loading by Assembly.LoadFrom is not the same [duplicate]
This question already has an answer here:
Two Types not equal that should be
4 answers
I'm creating type by name to put them to DI container. For any reason the DI container fails to register/resolve types created this way:
Type interfaceTypeFromAssembly = Assembly.LoadFrom(InterfacesAssemblyPath).GetTypes().First(t => t.Name == interfaceName);
Type implementorTypeFromAssembly = Assembly.LoadFrom(ApplicationAssemblyPath).GetTypes().First(t => t.Name == implementorClassName);
I compared types and found that:
Type implClassType= typeof(ImplClass);
Type implClassType2= typeof(ImplClass);
bool res = implClassType == implClassType2; // True
res = implClassType == implementorTypeFromAssembly; // False
The last line gives False although types have the same GUID.
What's the reason of such a behavior?
c# reflection dependency-injection
marked as duplicate by GSerg, Ofir Winegarten, Steven
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 14:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Two Types not equal that should be
4 answers
I'm creating type by name to put them to DI container. For any reason the DI container fails to register/resolve types created this way:
Type interfaceTypeFromAssembly = Assembly.LoadFrom(InterfacesAssemblyPath).GetTypes().First(t => t.Name == interfaceName);
Type implementorTypeFromAssembly = Assembly.LoadFrom(ApplicationAssemblyPath).GetTypes().First(t => t.Name == implementorClassName);
I compared types and found that:
Type implClassType= typeof(ImplClass);
Type implClassType2= typeof(ImplClass);
bool res = implClassType == implClassType2; // True
res = implClassType == implementorTypeFromAssembly; // False
The last line gives False although types have the same GUID.
What's the reason of such a behavior?
c# reflection dependency-injection
marked as duplicate by GSerg, Ofir Winegarten, Steven
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 14:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
What is the value of implementorTypeFromAssembly.ToString() ?
– Shai Aharoni
Nov 14 '18 at 11:42
@ShaiAharoni toString() for both objects give the same result
– amplifier
Nov 14 '18 at 12:04
add a comment |
This question already has an answer here:
Two Types not equal that should be
4 answers
I'm creating type by name to put them to DI container. For any reason the DI container fails to register/resolve types created this way:
Type interfaceTypeFromAssembly = Assembly.LoadFrom(InterfacesAssemblyPath).GetTypes().First(t => t.Name == interfaceName);
Type implementorTypeFromAssembly = Assembly.LoadFrom(ApplicationAssemblyPath).GetTypes().First(t => t.Name == implementorClassName);
I compared types and found that:
Type implClassType= typeof(ImplClass);
Type implClassType2= typeof(ImplClass);
bool res = implClassType == implClassType2; // True
res = implClassType == implementorTypeFromAssembly; // False
The last line gives False although types have the same GUID.
What's the reason of such a behavior?
c# reflection dependency-injection
This question already has an answer here:
Two Types not equal that should be
4 answers
I'm creating type by name to put them to DI container. For any reason the DI container fails to register/resolve types created this way:
Type interfaceTypeFromAssembly = Assembly.LoadFrom(InterfacesAssemblyPath).GetTypes().First(t => t.Name == interfaceName);
Type implementorTypeFromAssembly = Assembly.LoadFrom(ApplicationAssemblyPath).GetTypes().First(t => t.Name == implementorClassName);
I compared types and found that:
Type implClassType= typeof(ImplClass);
Type implClassType2= typeof(ImplClass);
bool res = implClassType == implClassType2; // True
res = implClassType == implementorTypeFromAssembly; // False
The last line gives False although types have the same GUID.
What's the reason of such a behavior?
This question already has an answer here:
Two Types not equal that should be
4 answers
c# reflection dependency-injection
c# reflection dependency-injection
asked Nov 14 '18 at 11:35
amplifieramplifier
495421
495421
marked as duplicate by GSerg, Ofir Winegarten, Steven
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 14:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by GSerg, Ofir Winegarten, Steven
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 14:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
What is the value of implementorTypeFromAssembly.ToString() ?
– Shai Aharoni
Nov 14 '18 at 11:42
@ShaiAharoni toString() for both objects give the same result
– amplifier
Nov 14 '18 at 12:04
add a comment |
What is the value of implementorTypeFromAssembly.ToString() ?
– Shai Aharoni
Nov 14 '18 at 11:42
@ShaiAharoni toString() for both objects give the same result
– amplifier
Nov 14 '18 at 12:04
What is the value of implementorTypeFromAssembly.ToString() ?
– Shai Aharoni
Nov 14 '18 at 11:42
What is the value of implementorTypeFromAssembly.ToString() ?
– Shai Aharoni
Nov 14 '18 at 11:42
@ShaiAharoni toString() for both objects give the same result
– amplifier
Nov 14 '18 at 12:04
@ShaiAharoni toString() for both objects give the same result
– amplifier
Nov 14 '18 at 12:04
add a comment |
1 Answer
1
active
oldest
votes
The types are different because typeof(ImplClass) loads type from assembly that is already loaded in app domain and Assembly.LoadFrom(InterfacesAssemblyPath) loads the assembly again. This means the Assembly object instance returned from Assembly.LoadFrom is new including new instances of all types.
There is no reason to use Assembly.LoadFrom when the assembly is already loaded in app domain. In this case you should acquire Assembly object from app domain. For example via Assembly.GetAssembly(SomeTypeInAssembly) or typeof(SomeTypeInAssembly).Assembly.
EDIT:
If you do not have any SomeTypeInAssembly or you do not want to use it you can use AppDomain.CurrentDomain.GetAssemblies().Single(assembly => assembly.GetName().Name == name).
Ok, but all that I have is class name, which is string. Should I do something like Type.GetType("ClassNameFromAssembly") and then pass is to GetAssembly?
– amplifier
Nov 14 '18 at 12:23
1
If you want to getAssemblyobject by name, you can useAppDomain.CurrentDomain.GetAssemblies().Single(assembly => assembly.GetName().Name == name)
– Martin Volek
Nov 14 '18 at 12:25
Thank you. Now it works! Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == "ServerApp.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); Type typeFromAssembly = assembly.GetType("ServerApp.UnitTests.MyType"); But I'm still wondering why var assembly = Assembly.GetAssembly(Type.GetType("ServerApp.UnitTests.MyType")); doesn't work?
– amplifier
Nov 14 '18 at 12:37
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The types are different because typeof(ImplClass) loads type from assembly that is already loaded in app domain and Assembly.LoadFrom(InterfacesAssemblyPath) loads the assembly again. This means the Assembly object instance returned from Assembly.LoadFrom is new including new instances of all types.
There is no reason to use Assembly.LoadFrom when the assembly is already loaded in app domain. In this case you should acquire Assembly object from app domain. For example via Assembly.GetAssembly(SomeTypeInAssembly) or typeof(SomeTypeInAssembly).Assembly.
EDIT:
If you do not have any SomeTypeInAssembly or you do not want to use it you can use AppDomain.CurrentDomain.GetAssemblies().Single(assembly => assembly.GetName().Name == name).
Ok, but all that I have is class name, which is string. Should I do something like Type.GetType("ClassNameFromAssembly") and then pass is to GetAssembly?
– amplifier
Nov 14 '18 at 12:23
1
If you want to getAssemblyobject by name, you can useAppDomain.CurrentDomain.GetAssemblies().Single(assembly => assembly.GetName().Name == name)
– Martin Volek
Nov 14 '18 at 12:25
Thank you. Now it works! Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == "ServerApp.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); Type typeFromAssembly = assembly.GetType("ServerApp.UnitTests.MyType"); But I'm still wondering why var assembly = Assembly.GetAssembly(Type.GetType("ServerApp.UnitTests.MyType")); doesn't work?
– amplifier
Nov 14 '18 at 12:37
add a comment |
The types are different because typeof(ImplClass) loads type from assembly that is already loaded in app domain and Assembly.LoadFrom(InterfacesAssemblyPath) loads the assembly again. This means the Assembly object instance returned from Assembly.LoadFrom is new including new instances of all types.
There is no reason to use Assembly.LoadFrom when the assembly is already loaded in app domain. In this case you should acquire Assembly object from app domain. For example via Assembly.GetAssembly(SomeTypeInAssembly) or typeof(SomeTypeInAssembly).Assembly.
EDIT:
If you do not have any SomeTypeInAssembly or you do not want to use it you can use AppDomain.CurrentDomain.GetAssemblies().Single(assembly => assembly.GetName().Name == name).
Ok, but all that I have is class name, which is string. Should I do something like Type.GetType("ClassNameFromAssembly") and then pass is to GetAssembly?
– amplifier
Nov 14 '18 at 12:23
1
If you want to getAssemblyobject by name, you can useAppDomain.CurrentDomain.GetAssemblies().Single(assembly => assembly.GetName().Name == name)
– Martin Volek
Nov 14 '18 at 12:25
Thank you. Now it works! Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == "ServerApp.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); Type typeFromAssembly = assembly.GetType("ServerApp.UnitTests.MyType"); But I'm still wondering why var assembly = Assembly.GetAssembly(Type.GetType("ServerApp.UnitTests.MyType")); doesn't work?
– amplifier
Nov 14 '18 at 12:37
add a comment |
The types are different because typeof(ImplClass) loads type from assembly that is already loaded in app domain and Assembly.LoadFrom(InterfacesAssemblyPath) loads the assembly again. This means the Assembly object instance returned from Assembly.LoadFrom is new including new instances of all types.
There is no reason to use Assembly.LoadFrom when the assembly is already loaded in app domain. In this case you should acquire Assembly object from app domain. For example via Assembly.GetAssembly(SomeTypeInAssembly) or typeof(SomeTypeInAssembly).Assembly.
EDIT:
If you do not have any SomeTypeInAssembly or you do not want to use it you can use AppDomain.CurrentDomain.GetAssemblies().Single(assembly => assembly.GetName().Name == name).
The types are different because typeof(ImplClass) loads type from assembly that is already loaded in app domain and Assembly.LoadFrom(InterfacesAssemblyPath) loads the assembly again. This means the Assembly object instance returned from Assembly.LoadFrom is new including new instances of all types.
There is no reason to use Assembly.LoadFrom when the assembly is already loaded in app domain. In this case you should acquire Assembly object from app domain. For example via Assembly.GetAssembly(SomeTypeInAssembly) or typeof(SomeTypeInAssembly).Assembly.
EDIT:
If you do not have any SomeTypeInAssembly or you do not want to use it you can use AppDomain.CurrentDomain.GetAssemblies().Single(assembly => assembly.GetName().Name == name).
edited Nov 14 '18 at 12:28
answered Nov 14 '18 at 12:09
Martin VolekMartin Volek
4141417
4141417
Ok, but all that I have is class name, which is string. Should I do something like Type.GetType("ClassNameFromAssembly") and then pass is to GetAssembly?
– amplifier
Nov 14 '18 at 12:23
1
If you want to getAssemblyobject by name, you can useAppDomain.CurrentDomain.GetAssemblies().Single(assembly => assembly.GetName().Name == name)
– Martin Volek
Nov 14 '18 at 12:25
Thank you. Now it works! Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == "ServerApp.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); Type typeFromAssembly = assembly.GetType("ServerApp.UnitTests.MyType"); But I'm still wondering why var assembly = Assembly.GetAssembly(Type.GetType("ServerApp.UnitTests.MyType")); doesn't work?
– amplifier
Nov 14 '18 at 12:37
add a comment |
Ok, but all that I have is class name, which is string. Should I do something like Type.GetType("ClassNameFromAssembly") and then pass is to GetAssembly?
– amplifier
Nov 14 '18 at 12:23
1
If you want to getAssemblyobject by name, you can useAppDomain.CurrentDomain.GetAssemblies().Single(assembly => assembly.GetName().Name == name)
– Martin Volek
Nov 14 '18 at 12:25
Thank you. Now it works! Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == "ServerApp.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); Type typeFromAssembly = assembly.GetType("ServerApp.UnitTests.MyType"); But I'm still wondering why var assembly = Assembly.GetAssembly(Type.GetType("ServerApp.UnitTests.MyType")); doesn't work?
– amplifier
Nov 14 '18 at 12:37
Ok, but all that I have is class name, which is string. Should I do something like Type.GetType("ClassNameFromAssembly") and then pass is to GetAssembly?
– amplifier
Nov 14 '18 at 12:23
Ok, but all that I have is class name, which is string. Should I do something like Type.GetType("ClassNameFromAssembly") and then pass is to GetAssembly?
– amplifier
Nov 14 '18 at 12:23
1
1
If you want to get
Assembly object by name, you can use AppDomain.CurrentDomain.GetAssemblies().Single(assembly => assembly.GetName().Name == name)– Martin Volek
Nov 14 '18 at 12:25
If you want to get
Assembly object by name, you can use AppDomain.CurrentDomain.GetAssemblies().Single(assembly => assembly.GetName().Name == name)– Martin Volek
Nov 14 '18 at 12:25
Thank you. Now it works! Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == "ServerApp.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); Type typeFromAssembly = assembly.GetType("ServerApp.UnitTests.MyType"); But I'm still wondering why var assembly = Assembly.GetAssembly(Type.GetType("ServerApp.UnitTests.MyType")); doesn't work?
– amplifier
Nov 14 '18 at 12:37
Thank you. Now it works! Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == "ServerApp.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); Type typeFromAssembly = assembly.GetType("ServerApp.UnitTests.MyType"); But I'm still wondering why var assembly = Assembly.GetAssembly(Type.GetType("ServerApp.UnitTests.MyType")); doesn't work?
– amplifier
Nov 14 '18 at 12:37
add a comment |
What is the value of implementorTypeFromAssembly.ToString() ?
– Shai Aharoni
Nov 14 '18 at 11:42
@ShaiAharoni toString() for both objects give the same result
– amplifier
Nov 14 '18 at 12:04