Type.GetType("ObjectName");

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

    #1

    Type.GetType("ObjectName");

    Hello all,

    I'm trying to do the above, pass in an object name and it return the
    type, but it's erroring, I've seen something like this before but
    can't quite remember. Can this be done?

    Thanks

    Jon

  • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

    #2
    RE: Type.GetType(&q uot;ObjectName& quot;);

    The following is lifted verbatim from the MSDN Documentation. Hopefully it
    will help clear things up:

    using System;
    namespace MyTypeNameSpace
    {
    class MyClass
    {
    public static void Main(string[] arg)
    {
    try
    {
    // Get the type of a specified class.
    Type myType1 = Type.GetType("S ystem.Int32");
    Console.WriteLi ne("The full name is {0}.", myType1.FullNam e);
    // Since NoneSuch does not exist in this assembly, GetType
    throws a TypeLoadExcepti on.
    Type myType2 = Type.GetType("N oneSuch", true);
    Console.WriteLi ne("The full name is {0}.", myType2.FullNam e);
    }
    catch(TypeLoadE xception e)
    {
    Console.WriteLi ne(e.Message);
    }
    catch(Exception e)
    {
    Console.WriteLi ne(e.Message);
    }
    }
    }
    }

    -- Peter
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "Jon" wrote:
    Hello all,
    >
    I'm trying to do the above, pass in an object name and it return the
    type, but it's erroring, I've seen something like this before but
    can't quite remember. Can this be done?
    >
    Thanks
    >
    Jon
    >
    >

    Comment

    • MasterGaurav \(www.edujini-labs.com\)

      #3
      Re: Type.GetType(&q uot;ObjectName& quot;);

      I'm trying to do the above, pass in an object name and it return the
      type, but it's erroring, I've seen something like this before but
      can't quite remember. Can this be done?
      What's the error?
      Unable to find the type? Check that it's the correct assembly from where you
      are loading...


      --
      Happy Hacking,
      Gaurav Vaish | www.mastergaurav.com


      -----------------------------------------


      Comment

      • kaza

        #4
        Re: Type.GetType(&q uot;ObjectName& quot;);

        On May 6, 2:17 pm, Jon <JonMYa...@gmai l.comwrote:
        Hello all,
        >
        I'm trying to do the above, pass in an object name and it return the
        type, but it's erroring, I've seen something like this before but
        can't quite remember. Can this be done?
        >
        Thanks
        >
        Jon
        hi , what is your environment ? are you doing it in asp.net and your
        cusotm objects if yes, the problem is that this object lives in
        another assembly whose name you do not know, if this is the this is a
        workaround, it works only if all classes are in same directory
        (app_code) which will be compiled to same dll

        //
        Type rootType = typeof (SomeClassFromA ppCode);
        string fullAssemblyNam e = rootType.Assemb ly.FullName;

        string fullClassName = typeName +", " + fullAssemblyNam e;
        Type type = Type.GetType(fu llClassName);


        Comment

        • MasterGaurav \(www.edujini-labs.com\)

          #5
          Re: Type.GetType(&q uot;ObjectName& quot;);

          Type rootType = typeof (SomeClassFromA ppCode);
          string fullAssemblyNam e = rootType.Assemb ly.FullName;
          >
          string fullClassName = typeName +", " + fullAssemblyNam e;
          Type type = Type.GetType(fu llClassName);

          Better use:

          rootType.Assemb ly.GetType(type Name)

          The code written above will be slower because of unnecessary overheads.


          --
          Happy Hacking,
          Gaurav Vaish | www.mastergaurav.com


          -----------------------------------------


          Comment

          Working...