Type loading by Assembly.LoadFrom is not the same [duplicate]










0
















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?










share|improve this question













marked as duplicate by GSerg, Ofir Winegarten, Steven c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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















0
















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?










share|improve this question













marked as duplicate by GSerg, Ofir Winegarten, Steven c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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













0












0








0









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?










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 11:35









amplifieramplifier

495421




495421




marked as duplicate by GSerg, Ofir Winegarten, Steven c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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

















  • 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












1 Answer
1






active

oldest

votes


















1














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






share|improve this answer

























  • 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 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

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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






share|improve this answer

























  • 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 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















1














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






share|improve this answer

























  • 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 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













1












1








1







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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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 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
















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



Popular posts from this blog

27

Top Tejano songwriter Luis Silva dead of heart attack at 64

Category:Rhetoric