❔ Weird Type comparison always false
The same
Type
of ILogger<> and a type provided by ParameterInfo
aren't matching despite them being the same type. Even IsAssignableTo/From aren't working either. Is there something I'm missing?21 Replies
output ^
essentially, i'm trying to discover which
parameterType
has an ILogger
interface so I can subsitute it from ILoggerFactory
why aren't you checking with ILogger<Something>
in the end that's what you are gonna use
because it's generic - I want "dynamic" loading of a logger type depending on the class I'm instantiating with Activator.CreateInstance
although, I've discovered they're using different paths for the
Microsoft.Extensions.Logging.Abstractions.dll
despite being the same version - must be something csproj related with referencesIt prints out like types are same but its determined by CLR and can be different even output says the same when you print
Can you try typeA.Equals(typeB) ?
it checks underlying system type which should be more accurate
those dirs printed above were from
(type).Module.FullyQualifiedName
which is why they're not assignable
I do utilise
in the csproj of <project>
thoughyou might want to use Type.UnderlyingSystemType for generic types (GetGenericTypeDefinition is fine though)
it ignores the generic part
ooh, hadn't considered that
will give that a whirl
still nope - again, i think it's because it's loading the ILogger for its local Logging.Abstractions dll in one project, and the other project is also using it's own
there are some extensions might be useful
find interfaces also will not be the same as the modules are different
that's the core issue
well yes if you compare two Types named same but referenced from different assemblies, types will be different
oh I see - won't the Type of the interface be different though?
parameterTypes are like
ILogger<Something>
right ? If ILogger is Microsoft.Extensions.Logging.ILogger and both reference are from the same dll then equality check sohuld be true with use of GetGenericTypeDefinitionthey don't
again, the
type.Module
s are different, therefore the equality match is false
i've tried with GetGenericTypeDefinitionyou can use another project named like Project.Common and use that reference in both. That way module will be same
or you can check the name equality
nah that won't work either
essentially i have:
the aim being Bar will create Foo if the Foo wants an ILogger
Now I don't want Foo to have access to the service provider, but a "vetted" subset of items that I want it allowed access to such as the ILogger
Where does Foo can access the IServiceProvider if it doesnt have constructor accepts that parameter. Event if it has consturctor that take else than ILogger, it will be passed null
If you have control over both project, you can use factory like FooProvider, BarProvider and manage instantiation in there rather than controling Types and Reflection
Unfortunately I don't - I have access 5o Project.Two, but not One 😔
But fair enough - I'll keep trying
This was fixed by changing
to
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.