Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic class with itself as argument of base type equality problem #119

Open
Xriuk opened this issue Jun 5, 2024 · 3 comments
Open

Generic class with itself as argument of base type equality problem #119

Xriuk opened this issue Jun 5, 2024 · 3 comments

Comments

@Xriuk
Copy link

Xriuk commented Jun 5, 2024

Hello, I don't know if I'm missing something here (about your library or Roslyn), but until know I had compared all class types by equality:

classType1 = // some code which returns IClass

classType2 = // some other code which returns IClass

if classType1 == classType2
    }} same {{
end

However if I have a class like this in C#:

public class Base<T>{}

public class Derived : Base<Derived>{}

This does not work:

classType = // some code which returns IClass = Derived

if classType.BaseType.IsGeneric && classType.BaseType.TypeArguments[0] == classType
    }} same {{
end

So apparently Derived and its parent generic argument Derived are different types, why?

@NeVeSpl
Copy link
Owner

NeVeSpl commented Jun 5, 2024

It is on NTypewriter side, the equality operator is not overridden, so standard reference comparison is used, and since an instance of object representing a given type is not cached, the results are exactly as you described.

@Xriuk
Copy link
Author

Xriuk commented Jun 5, 2024

Oh, so types are not cached like System.Type? Is there an alternative to compare two given types?

@NeVeSpl
Copy link
Owner

NeVeSpl commented Jun 6, 2024

They are not, probably only by using their FullName right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants