Get the class of a Type C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nelsonbrodyk
    New Member
    • Mar 2008
    • 81

    Get the class of a Type C#

    Hey All,
    I was just wondering if this is possible.

    I have a List<x> object, and from this, I need to be able to get x. and then use x's class as the generic type for another method.

    ie.

    private R GetData<R>() {


    //this should get me the type of the generic. ie List<string>, this should return //string
    Type listType = data.GetType(). GetGenericArgum ents()[0];

    //Call another method with the generic type of the list
    MethodB<listTyp e>(args);

    }

    How can this be done? Right now, I cannot specify the type in MethodB, as I think it wants the class. Is there any way to do this?

    Thanks.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    All objects have a .GetType() that returns the Type for the object that they are.

    Comment

    • nelsonbrodyk
      New Member
      • Mar 2008
      • 81

      #3
      When I try and call MethodB: MethodB<listTyp e>, the compiler does not allow this, because it says:
      Error 2 The type or namespace name 'listType' could not be found (are you missing a using directive or an assembly reference?

      I think it's because it's expecting a class, like "string", "int" etc, I can't give it a type can I?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well lets say you have:
        Code:
        Myclass fred = new Myclass();
        
        MethodB<typeof(fred)>
        //or
        MethodB<fred.GetType()>
        Either of those should work.

        Comment

        • nelsonbrodyk
          New Member
          • Mar 2008
          • 81

          #5
          Here is what I am trying to say, because I am not following your reply.

          I have a list of string. ie. List<string>.

          I want to call a method such that:
          MethodB<string> ();

          But I won't know what the list type is until runtime. ie.

          List<?> myList;

          MethodB<?>;

          If I get the generic Type of the list, that returns me a Type object.
          ie. Type listType = data.GetType(). GetGenericArgum ents()[0];

          You cannot go List<listType>, because it seems to expect a class only.

          Does that make sense?

          Comment

          • nelsonbrodyk
            New Member
            • Mar 2008
            • 81

            #6
            Looks like you can do this through reflection and the calling of your own method. Anyone know of a more elegant solution?

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Ok I see what is going on then. Yes, you should be able to do that using the System.Reflecti on namespace I think?

              Comment

              • nelsonbrodyk
                New Member
                • Mar 2008
                • 81

                #8
                Ya, what I did was basically call the method again via reflection. I'm guessing that's really the only way to do it.

                Thanks,
                Nelson

                Comment

                Working...