Enumerate all Types

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

    #1

    Enumerate all Types

    I have an odd question that I'm hoping someone can help with.
    I simply (or not simply?) need to enumerate through all of the data types in
    the .Net framework. I do understand that if this is possible, it will take
    a long time, and I really don't care :)
    I need to go through all of the types, look for something within each, and
    do something with it if it contains what i'm looking for.
    So, is it possible to do this enumeration? Let me know...

    Thanks
    -Cliff


  • Marco Martin

    #2
    Re: Enumerate all Types

    hi Cliff.

    You should be able to do this using the enum "TypeCode". This enumeration
    holds all of the "standard" types used in .NET

    hope this helps

    Marco

    "Cliff Harris" <hersh@myrealbo x.com> wrote in message
    news:ufxz%23IOw DHA.1856@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > I have an odd question that I'm hoping someone can help with.
    > I simply (or not simply?) need to enumerate through all of the data types[/color]
    in[color=blue]
    > the .Net framework. I do understand that if this is possible, it will[/color]
    take[color=blue]
    > a long time, and I really don't care :)
    > I need to go through all of the types, look for something within each, and
    > do something with it if it contains what i'm looking for.
    > So, is it possible to do this enumeration? Let me know...
    >
    > Thanks
    > -Cliff
    >
    >[/color]


    Comment

    • bullshark

      #3
      Re: Enumerate all Types

      On Fri, 12 Dec 2003 14:00:53 -0500, "Cliff Harris" <hersh@myrealbo x.com> wrote:
      [color=blue]
      >I have an odd question that I'm hoping someone can help with.
      >I simply (or not simply?) need to enumerate through all of the data types in
      >the .Net framework. I do understand that if this is possible, it will take
      >a long time, and I really don't care :)
      >I need to go through all of the types, look for something within each, and
      >do something with it if it contains what i'm looking for.[/color]

      This was installed with my vsn03:
      C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\S amples\Applicat ions\TypeFinder

      It's a sample project that produces a very useful utility with the following command line:

      FindType [SystemOptions] [MatchOptions] [ShowOptions] [MiscOptions] Name

      where SystemOptions
      d:[Directory] - Additional directory to search
      where MatchOptions
      x - Name = Exact Type Name (including namespace)
      n - Name = Namespace
      w - Match the name anywhere in the namespace
      where ShowOptions
      i - Show Interfaces
      f - Show Fields
      p - Show Properties
      e - Show Events
      m - Show Methods
      a - Show All Info
      l - Show Module Information
      where MiscOptions
      v = Verbose
      r = For every type find display base type information
      ? = Prints Usage information

      Examples

      FindType String
      Finds all types that have 'String' as part of their type name

      FindType -r String
      Finds all types that have 'String' as part of their name
      and prints out all base classes

      FindType -n System.Reflecti on
      Displays all types in the 'System.Reflect ion' namespace

      FindType -xipm System.String
      Displays the interfaces, properties and methods on the 'System.String' type

      FindType -d:C:\ -d:"C:\Program Files\Microsoft .NET\FrameworkS DK\Lib" String
      Searches DLLs C:\ and C:\Program Files\Microsoft .NET\FrameworkS DK\Lib
      as well as in the current directory

      It ought to do a bang up job of showing how to get what you need.

      regards,

      bullshark

      Comment

      • Eric Gunnerson [MS]

        #4
        Re: Enumerate all Types

        Here's some code I wrote a while back to do this and look at exceptions.
        There's lots of junk in there, but the basic framework should work.

        // ExceptionSurvey
        // This utility looks through all the assemblies in a specified directory
        // (or the current diretory if there is no specified directory), and checks
        // to make sure all exception classes follow the specified guidelines.
        //
        // It verifies:
        // 1) The exception name ends in "Exception" ...
        // 2) The exception implements the 3 standard constructors:
        // class();
        // class(string message);
        // class(string message, Exception inner);
        // 3) The exception implements the deserialization constructor:
        // class(Serializa tionInfo info, StreamingContex t context)
        // 4) The exception has no public fields
        // 5) If the exception has private fields, that it implements
        GetObjectData()
        // (there's no guarantee it does it *correctly*.
        // 6) If the exception has private fields, it overrides the Message
        property.
        // 7) The exception is marked as serializable.


        // This define controls whether the program tries to create, serialize, and
        deserialize
        // each exception class. Because of the assembly lookup rules, this will
        only work if the
        // exception checker is in the same directory as the assembly being checked.
        // It's therefore turned off in the default case.
        //#define LIVETEST


        using System;
        using System.IO;
        using System.Reflecti on;
        using System.Collecti ons;
        using System.Runtime. Serialization;
        using System.Runtime. Serialization.F ormatters.Binar y;

        namespace ExceptionSurvey
        {
        /// <summary>
        /// Summary description for Class1.
        /// </summary>
        class ExceptionTester
        {
        int count = 0;
        int errorCantSerial ize = 0;
        int errorNoInnerCon structor = 0;
        int errorNoSerializ ationConstructo r = 0;
        int errorMissedFiel ds = 0;

        // test whether an exception can be serialized and deserialized by doing
        it.
        bool IsSerializable( Assembly assembly, Type type)
        {
        object o = null;
        try
        {
        o = assembly.Create Instance(type.N ame);
        }
        catch (MissingMethodE xception e)
        {
        Console.WriteLi ne(e);
        Console.WriteLi ne("Can't test serialization - no () constructor");
        return(true);
        }
        Stream streamRead = null;
        try
        {
        // try to serialize
        try
        {
        Stream streamWrite = File.Create("My Row.bin");
        BinaryFormatter binaryWrite = new BinaryFormatter ();
        binaryWrite.Ser ialize(streamWr ite, o);
        streamWrite.Clo se();
        }
        catch (Exception e)
        {
        Console.WriteLi ne("Can't Serialize: {0}", e);
        return false;
        }

        // try to deserialize
        try
        {
        streamRead = File.OpenRead(" MyRow.bin");
        BinaryFormatter binaryRead = new BinaryFormatter ();
        object oout = binaryRead.Dese rialize(streamR ead);
        }
        catch (Exception e)
        {
        Console.WriteLi ne(" Can't Deserialize: {0}", e);
        return false;
        }
        }
        finally
        {
        streamRead.Clos e();
        }

        return(true);
        }

        bool IsException(Typ e type)
        {
        Type baseType = null;

        while ((baseType = type.BaseType) != null)
        {
        if (baseType == typeof(System.E xception))
        return(true);
        type = baseType;
        }

        return(false);
        }

        public bool FindConstructor (Type t, string desc, params Type[] parameters)
        {
        bool retval = false;

        ConstructorInfo ci = t.GetConstructo r(BindingFlags. NonPublic |
        BindingFlags.Pu blic | BindingFlags.In stance, null, parameters, null);
        string message = null;
        if (ci != null)
        {
        if (ci.IsPublic)
        retval = true;
        else if (ci.IsPrivate)
        message = "Private " + t.Name + desc;
        else if (ci.IsFamily)
        message = "Internal " + t.Name + desc;
        }
        else
        {
        message = "Missing " + t.Name + desc;
        retval = false;
        }

        if (message != null)
        Console.WriteLi ne(" " + message);
        return retval;
        }

        void CheckException( Assembly assembly, Type t)
        {
        count++;
        Console.WriteLi ne(" {0}", t);

        // check to see that the exception is correctly named, with
        // "Exception" at the end.

        if (t.Name.LastInd exOf("Exception ") + "Exception".Len gth !=
        t.Name.Length)
        {
        Console.WriteLi ne(" Improper Name: {0}", t.Name);
        }

        // Does the exception have the 3 standard constructors?

        // Default constructor
        FindConstructor (t, "()");

        // Constructor with a single string parameter
        FindConstructor ( t,
        "(string message)",
        typeof(System.S tring));

        // Constructor with a string and an inner exception
        if (!FindConstruct or( t,
        "(string message, Exception inner)",
        typeof(System.S tring),
        typeof(System.E xception)))
        {
        this.errorNoInn erConstructor++ ;
        }

        // check to see if the serialization constructor is present...
        if (!FindConstruct or( t,
        "(Serialization Info info, StreamingContex t context)",
        typeof(System.R untime.Serializ ation.Serializa tionInfo),
        typeof(System.R untime.Serializ ation.Streaming Context)))
        {
        this.errorNoSer ializationConst ructor++;
        }

        // check to see if the type is market as serializable
        if (!t.IsSerializa ble)
        {
        Console.WriteLi ne(" Exception isn't serializable - missing
        [Serializable]?");
        }

        // The following block is #ifed out because it requires that the
        exception
        // checking exe be in the same directory as the assembly to be checked.
        #if LIVETEST
        // check to see whether this exception can be successfully serialized and
        deserialized
        if (!IsSerializabl e(assembly, t))
        {
        Console.WriteLi ne(" Not serializable");
        this.errorCantS erialize++;
        }
        #endif

        // check to see if there are any public fields. These should be
        properties instead...
        FieldInfo[] publicFields = t.GetFields(Bin dingFlags.Decla redOnly |
        BindingFlags.Pu blic | BindingFlags.In stance);
        if (publicFields.L ength != 0)
        {
        foreach (FieldInfo fieldInfo in publicFields)
        {
        Console.WriteLi ne(" public field {0} - should expose through
        property", fieldInfo.Name) ;
        }
        }

        // If this exception has any fields, check to
        // make sure it has a version of GetObjectData. If not,
        // it does't serialize those fields...
        FieldInfo[] fields = t.GetFields(Bin dingFlags.Decla redOnly |
        BindingFlags.No nPublic | BindingFlags.Pu blic | BindingFlags.In stance);
        if (fields.Length != 0)
        {
        if (t.GetMethod("G etObjectData", BindingFlags.De claredOnly |
        BindingFlags.Pu blic | BindingFlags.In stance) == null)
        {
        this.errorMisse dFields++;
        Console.WriteLi ne(" Inherits GetObjectData() ");
        Console.WriteLi ne(" Doesn't serialize these fields");
        foreach (FieldInfo field in fields)
        {
        Console.WriteLi ne(" {0}", field);
        }
        }
        // Make sure Message is overridden
        if (t.GetProperty( "Message", BindingFlags.De claredOnly |
        BindingFlags.Pu blic | BindingFlags.In stance) == null)
        {
        Console.WriteLi ne(" Exception doesn't override the Message
        property");
        }
        }

        Console.WriteLi ne();
        }

        void ProcessAssembly (FileInfo file)
        {
        try
        {
        Assembly a = Assembly.LoadFr om(file.FullNam e);

        Console.WriteLi ne("File: {0}", file.FullName);

        foreach (Type t in a.GetTypes())
        {
        if (IsException(t) )
        {
        CheckException( a, t);
        }
        }
        //Environment.Exi t(0);


        }
        catch (Exception)
        {
        }

        }

        void PrintErrors()
        {
        Console.WriteLi ne("{0} Exceptions processed", count);
        Console.WriteLi ne("{0} can't be serialized", errorCantSerial ize);
        Console.WriteLi ne("{0} can't be wrapped", errorNoInnerCon structor);
        Console.WriteLi ne("{0} have no serialization constructor",
        errorNoSerializ ationConstructo r);
        Console.WriteLi ne("{0} don't serialize fields", errorMissedFiel ds);
        }

        static void Main(string[] args)
        {

        ExceptionTester tester = new ExceptionTester ();

        DirectoryInfo dirInfo = new DirectoryInfo(" .");
        foreach (FileInfo file in dirInfo.GetFile s("*.dll"))
        {
        tester.ProcessA ssembly(file);
        }
        tester.PrintErr ors();
        }

        }
        }


        --
        Eric Gunnerson

        Visit the C# product team at http://www.csharp.net
        Eric's blog is at http://blogs.gotdotnet.com/ericgu/

        This posting is provided "AS IS" with no warranties, and confers no rights.
        "Cliff Harris" <hersh@myrealbo x.com> wrote in message
        news:ufxz%23IOw DHA.1856@TK2MSF TNGP09.phx.gbl. ..[color=blue]
        > I have an odd question that I'm hoping someone can help with.
        > I simply (or not simply?) need to enumerate through all of the data types[/color]
        in[color=blue]
        > the .Net framework. I do understand that if this is possible, it will[/color]
        take[color=blue]
        > a long time, and I really don't care :)
        > I need to go through all of the types, look for something within each, and
        > do something with it if it contains what i'm looking for.
        > So, is it possible to do this enumeration? Let me know...
        >
        > Thanks
        > -Cliff
        >
        >[/color]


        Comment

        Working...