GetType and Interfaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grande

    GetType and Interfaces

    Hi all,

    I'm trying to use GetType to get an Interface, but it's not working?
    Is there a special way you have to do this, since it's an Interface
    (rather than a complete class)?

    Code:
    Type SpecDataType = SpecData.GetTyp e();
    string strTemp = "SCG.Persistenc e.Interfaces.Pr oduct.I" +
    SpecDataType.Na me + "Dao";
    //string strTemp = "I" + SpecDataType.Na me + "Dao";
    Type SpecDataInterfa ceType = Type.GetType(st rTemp);

    Thanks!

  • Jon Skeet [C# MVP]

    #2
    Re: GetType and Interfaces

    Grande <matt.grande@gm ail.comwrote:
    I'm trying to use GetType to get an Interface, but it's not working?
    Is there a special way you have to do this, since it's an Interface
    (rather than a complete class)?
    >
    Code:
    Type SpecDataType = SpecData.GetTyp e();
    string strTemp = "SCG.Persistenc e.Interfaces.Pr oduct.I" +
    SpecDataType.Na me + "Dao";
    //string strTemp = "I" + SpecDataType.Na me + "Dao";
    Type SpecDataInterfa ceType = Type.GetType(st rTemp);
    Could you post a short but complete program which demonstrates the
    problem?

    See http://www.pobox.com/~skeet/csharp/complete.html for details of
    what I mean by that.

    Note that Type.GetType needs the full assembly name (version
    information etc) unless you're getting a type from the calling assembly
    or mscorlib.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Stanimir Stoyanov

      #3
      RE: GetType and Interfaces

      Hello Grande,

      The Type.GetType method requires you to type the Assembly fully qualified
      name of the Type, which means you have to include the assembly name, version,
      culture and public key token (if any). For instance,
      "System.Int 32, mscorlib, Version=2.0.0.0 , Culture=neutral ,
      PublicKeyToken= b77a5c561934e08 9" to obtain the Type.Int32 instance from .NET
      Framework v2.0.

      --
      Best regards,

      Stanimir Stoyanov
      admin@nospam.st oyanoff.info


      "Grande" wrote:
      Hi all,
      >
      I'm trying to use GetType to get an Interface, but it's not working?
      Is there a special way you have to do this, since it's an Interface
      (rather than a complete class)?
      >
      Code:
      Type SpecDataType = SpecData.GetTyp e();
      string strTemp = "SCG.Persistenc e.Interfaces.Pr oduct.I" +
      SpecDataType.Na me + "Dao";
      //string strTemp = "I" + SpecDataType.Na me + "Dao";
      Type SpecDataInterfa ceType = Type.GetType(st rTemp);
      >
      Thanks!
      >
      >

      Comment

      • Jon Skeet [C# MVP]

        #4
        RE: GetType and Interfaces

        Stanimir Stoyanov <admin@nospam.s toyanoff.infowr ote:
        The Type.GetType method requires you to type the Assembly fully qualified
        name of the Type, which means you have to include the assembly name, version,
        culture and public key token (if any). For instance,
        "System.Int 32, mscorlib, Version=2.0.0.0 , Culture=neutral ,
        PublicKeyToken= b77a5c561934e08 9" to obtain the Type.Int32 instance from .NET
        Framework v2.0.
        Note that that's only true if the type isn't in the calling assembly or
        mscorlib. For example, Type.GetType ("System.Int32" ) will work fine for
        your example, as Int32 is in mscorlib.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Stanimir Stoyanov

          #5
          RE: GetType and Interfaces

          Thank you for pointing this out, Jon.
          --
          Best regards,

          Stanimir Stoyanov
          admin@nospam.st oyanoff.info


          "Jon Skeet [C# MVP]" wrote:
          Stanimir Stoyanov <admin@nospam.s toyanoff.infowr ote:
          The Type.GetType method requires you to type the Assembly fully qualified
          name of the Type, which means you have to include the assembly name, version,
          culture and public key token (if any). For instance,
          "System.Int 32, mscorlib, Version=2.0.0.0 , Culture=neutral ,
          PublicKeyToken= b77a5c561934e08 9" to obtain the Type.Int32 instance from .NET
          Framework v2.0.
          >
          Note that that's only true if the type isn't in the calling assembly or
          mscorlib. For example, Type.GetType ("System.Int32" ) will work fine for
          your example, as Int32 is in mscorlib.
          >
          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too
          >

          Comment

          • Leon Lambert

            #6
            Re: GetType and Interfaces

            Grande wrote:
            Hi all,
            >
            I'm trying to use GetType to get an Interface, but it's not working?
            Is there a special way you have to do this, since it's an Interface
            (rather than a complete class)?
            >
            Code:
            Type SpecDataType = SpecData.GetTyp e();
            string strTemp = "SCG.Persistenc e.Interfaces.Pr oduct.I" +
            SpecDataType.Na me + "Dao";
            //string strTemp = "I" + SpecDataType.Na me + "Dao";
            Type SpecDataInterfa ceType = Type.GetType(st rTemp);
            >
            Thanks!
            >
            Not exactly sure what you want but the following works for me.
            It gets the interface 2 ways. The first way is if it is part of the
            loaded assembly. The second is if you used reflection to load an
            assembly like Assembly.LoadFr om and want to get the interface from it.
            Note in both cases you have to use the fully qualified name including
            the namespace.

            namespace TestNamespace
            {
            interface TestInterface
            {
            void DoSomething();
            }
            class TestClass
            {
            [STAThread]
            static void Main(string[] args)
            {
            Type ts = Type.GetType("T estNamespace.Te stInterface");
            Assembly asem = ts.Assembly;
            Type ts2 = asem.GetType("T estNamespace.Te stInterface");
            }
            }
            }

            Comment

            Working...