Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Type Overflow...

Type Overflow...

Scheduled Pinned Locked Moved C#
csharpcomdata-structuresquestion
5 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bernhard Hiller
    wrote on last edited by
    #1

    I have an interface and a type implementing it:

    public interface IValueWithUnit {...}
    [Serializable]
    public struct DoubleValueWithUnit : IValueWithUnit { ... }

    At a different place, I have a string with the name of the type, and must find out if the type derives from above interface:

    string name = "DoubleValueWithUnit";
    Type t;
    if (TypeHelper.TryFindType(name, out t))
    {
    m_IsValueWithUnit = t.IsSubclassOf(typeof(IValueWithUnit)); // false
    m_IsValueWithUnit = t.IsSubclassOf(typeof(DoubleValueWithUnit)); // false
    m_IsValueWithUnit = t.IsInstanceOfType(typeof(IValueWithUnit)); // false
    m_IsValueWithUnit = t.IsInstanceOfType(typeof(DoubleValueWithUnit)); // false
    m_IsValueWithUnit = t.IsAssignableFrom(typeof(IValueWithUnit)); // false
    m_IsValueWithUnit = t.IsAssignableFrom(typeof(DoubleValueWithUnit)); // true
    m_IsValueWithUnit = t.GetInterface(nameof(IValueWithUnit)) != null; // true

    The TryFindType function is from c# - Type.GetType("namespace.a.b.ClassName") returns null - Stack Overflow[^]. You see the many "false" comments? DoubleValueWithUnit is neither a subclass nor an instance of DoubleValueWithUnit or IValueWithUnit, and is also not assignable from IValueWithUnit, but at least it is assignable from DoubleValueWithUnit. I am confused now: my brain threw a TypeOverflowException.

    Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

    P Richard DeemingR 2 Replies Last reply
    0
    • B Bernhard Hiller

      I have an interface and a type implementing it:

      public interface IValueWithUnit {...}
      [Serializable]
      public struct DoubleValueWithUnit : IValueWithUnit { ... }

      At a different place, I have a string with the name of the type, and must find out if the type derives from above interface:

      string name = "DoubleValueWithUnit";
      Type t;
      if (TypeHelper.TryFindType(name, out t))
      {
      m_IsValueWithUnit = t.IsSubclassOf(typeof(IValueWithUnit)); // false
      m_IsValueWithUnit = t.IsSubclassOf(typeof(DoubleValueWithUnit)); // false
      m_IsValueWithUnit = t.IsInstanceOfType(typeof(IValueWithUnit)); // false
      m_IsValueWithUnit = t.IsInstanceOfType(typeof(DoubleValueWithUnit)); // false
      m_IsValueWithUnit = t.IsAssignableFrom(typeof(IValueWithUnit)); // false
      m_IsValueWithUnit = t.IsAssignableFrom(typeof(DoubleValueWithUnit)); // true
      m_IsValueWithUnit = t.GetInterface(nameof(IValueWithUnit)) != null; // true

      The TryFindType function is from c# - Type.GetType("namespace.a.b.ClassName") returns null - Stack Overflow[^]. You see the many "false" comments? DoubleValueWithUnit is neither a subclass nor an instance of DoubleValueWithUnit or IValueWithUnit, and is also not assignable from IValueWithUnit, but at least it is assignable from DoubleValueWithUnit. I am confused now: my brain threw a TypeOverflowException.

      Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

      P Offline
      P Offline
      phil o
      wrote on last edited by
      #2

      What is the relation between the name variable, and the m_Setting.Type property? Here's what I can understand from the results you get:

      • DoubleValueWithUnit is not a subclass of IValueWithUnit. An interface implementation is not considered as inheritance.
      • DoubleValueWithUnit is not a subclass of DoubleValueWithUnit. It is the very same class.
      • DoubleValueWithUnit is not an instance of IValueWithUnit. It is the type itself, not an instance of the type.
      • DoubleValueWithUnit is not an instance of DoubleValueWithUnit. Same comment as above.
      • DoubleValueWithUnit is not assignable from IValueWithUnit. It implements the interface, but that does not mean that every implementation of the interface should allow to cast to actual type. There could be some properties/methods of the class which are not part of the interface.
      • DoubleValueWithUnit is assignable from DoubleValueWithUnit. This one seems logical.
      • DoubleValueWithUnit implements IValueWithUnit. Thus IValueWithUnit is an element of the list of interfaces implemented by DoubleValueWithUnit.

      "Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."

      B 1 Reply Last reply
      0
      • P phil o

        What is the relation between the name variable, and the m_Setting.Type property? Here's what I can understand from the results you get:

        • DoubleValueWithUnit is not a subclass of IValueWithUnit. An interface implementation is not considered as inheritance.
        • DoubleValueWithUnit is not a subclass of DoubleValueWithUnit. It is the very same class.
        • DoubleValueWithUnit is not an instance of IValueWithUnit. It is the type itself, not an instance of the type.
        • DoubleValueWithUnit is not an instance of DoubleValueWithUnit. Same comment as above.
        • DoubleValueWithUnit is not assignable from IValueWithUnit. It implements the interface, but that does not mean that every implementation of the interface should allow to cast to actual type. There could be some properties/methods of the class which are not part of the interface.
        • DoubleValueWithUnit is assignable from DoubleValueWithUnit. This one seems logical.
        • DoubleValueWithUnit implements IValueWithUnit. Thus IValueWithUnit is an element of the list of interfaces implemented by DoubleValueWithUnit.

        "Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        phil.o wrote:

        What is the relation between the name variable, and the m_Setting.Type property?

        Oh, I forgot to replace that; question updated. Thanks for your explanation!

        Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

        1 Reply Last reply
        0
        • B Bernhard Hiller

          I have an interface and a type implementing it:

          public interface IValueWithUnit {...}
          [Serializable]
          public struct DoubleValueWithUnit : IValueWithUnit { ... }

          At a different place, I have a string with the name of the type, and must find out if the type derives from above interface:

          string name = "DoubleValueWithUnit";
          Type t;
          if (TypeHelper.TryFindType(name, out t))
          {
          m_IsValueWithUnit = t.IsSubclassOf(typeof(IValueWithUnit)); // false
          m_IsValueWithUnit = t.IsSubclassOf(typeof(DoubleValueWithUnit)); // false
          m_IsValueWithUnit = t.IsInstanceOfType(typeof(IValueWithUnit)); // false
          m_IsValueWithUnit = t.IsInstanceOfType(typeof(DoubleValueWithUnit)); // false
          m_IsValueWithUnit = t.IsAssignableFrom(typeof(IValueWithUnit)); // false
          m_IsValueWithUnit = t.IsAssignableFrom(typeof(DoubleValueWithUnit)); // true
          m_IsValueWithUnit = t.GetInterface(nameof(IValueWithUnit)) != null; // true

          The TryFindType function is from c# - Type.GetType("namespace.a.b.ClassName") returns null - Stack Overflow[^]. You see the many "false" comments? DoubleValueWithUnit is neither a subclass nor an instance of DoubleValueWithUnit or IValueWithUnit, and is also not assignable from IValueWithUnit, but at least it is assignable from DoubleValueWithUnit. I am confused now: my brain threw a TypeOverflowException.

          Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          As Phil explained, you've got the IsAssignableFrom the wrong way round:

          m_IsValueWithUnit = typeof(IValueWithUnit).IsAssignableFrom(t); // true

          But rest assured you're not alone in being confused by that method. :) Nick Craver on Twitter: "Anyone else pretty reliably get .IsAssignableFrom() backwards?"[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          B 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            As Phil explained, you've got the IsAssignableFrom the wrong way round:

            m_IsValueWithUnit = typeof(IValueWithUnit).IsAssignableFrom(t); // true

            But rest assured you're not alone in being confused by that method. :) Nick Craver on Twitter: "Anyone else pretty reliably get .IsAssignableFrom() backwards?"[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            B Offline
            B Offline
            Bernhard Hiller
            wrote on last edited by
            #5

            Thanks. Good to know that.

            Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups