473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Nov 15 '05 #1
3 2296
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" <he***@myrealbox.com> wrote in message
news:uf****************@TK2MSFTNGP09.phx.gbl...
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

Nov 15 '05 #2
On Fri, 12 Dec 2003 14:00:53 -0500, "Cliff Harris" <he***@myrealbox.com> wrote:
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.


This was installed with my vsn03:
C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Samples\Applications\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.Reflection
Displays all types in the 'System.Reflection' 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\FrameworkSDK\Lib" String
Searches DLLs C:\ and C:\Program Files\Microsoft.NET\FrameworkSDK\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

Nov 15 '05 #3
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(SerializationInfo info, StreamingContext 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.Reflection;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ExceptionSurvey
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class ExceptionTester
{
int count = 0;
int errorCantSerialize = 0;
int errorNoInnerConstructor = 0;
int errorNoSerializationConstructor = 0;
int errorMissedFields = 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.CreateInstance(type.Name);
}
catch (MissingMethodException e)
{
Console.WriteLine(e);
Console.WriteLine("Can't test serialization - no () constructor");
return(true);
}
Stream streamRead = null;
try
{
// try to serialize
try
{
Stream streamWrite = File.Create("MyRow.bin");
BinaryFormatter binaryWrite = new BinaryFormatter();
binaryWrite.Serialize(streamWrite, o);
streamWrite.Close();
}
catch (Exception e)
{
Console.WriteLine("Can't Serialize: {0}", e);
return false;
}

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

return(true);
}

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

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

return(false);
}

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

ConstructorInfo ci = t.GetConstructor(BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance, 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.WriteLine(" " + message);
return retval;
}

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

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

if (t.Name.LastIndexOf("Exception") + "Exception".Length !=
t.Name.Length)
{
Console.WriteLine(" 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.String));

// Constructor with a string and an inner exception
if (!FindConstructor( t,
"(string message, Exception inner)",
typeof(System.String),
typeof(System.Exception)))
{
this.errorNoInnerConstructor++;
}

// check to see if the serialization constructor is present...
if (!FindConstructor( t,
"(SerializationInfo info, StreamingContext context)",
typeof(System.Runtime.Serialization.SerializationI nfo),
typeof(System.Runtime.Serialization.StreamingConte xt)))
{
this.errorNoSerializationConstructor++;
}

// check to see if the type is market as serializable
if (!t.IsSerializable)
{
Console.WriteLine(" 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 (!IsSerializable(assembly, t))
{
Console.WriteLine(" Not serializable");
this.errorCantSerialize++;
}
#endif

// check to see if there are any public fields. These should be
properties instead...
FieldInfo[] publicFields = t.GetFields(BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.Instance);
if (publicFields.Length != 0)
{
foreach (FieldInfo fieldInfo in publicFields)
{
Console.WriteLine(" 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(BindingFlags.DeclaredOnly |
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (fields.Length != 0)
{
if (t.GetMethod("GetObjectData", BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.Instance) == null)
{
this.errorMissedFields++;
Console.WriteLine(" Inherits GetObjectData()");
Console.WriteLine(" Doesn't serialize these fields");
foreach (FieldInfo field in fields)
{
Console.WriteLine(" {0}", field);
}
}
// Make sure Message is overridden
if (t.GetProperty("Message", BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.Instance) == null)
{
Console.WriteLine(" Exception doesn't override the Message
property");
}
}

Console.WriteLine();
}

void ProcessAssembly(FileInfo file)
{
try
{
Assembly a = Assembly.LoadFrom(file.FullName);

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

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

}

void PrintErrors()
{
Console.WriteLine("{0} Exceptions processed", count);
Console.WriteLine("{0} can't be serialized", errorCantSerialize);
Console.WriteLine("{0} can't be wrapped", errorNoInnerConstructor);
Console.WriteLine("{0} have no serialization constructor",
errorNoSerializationConstructor);
Console.WriteLine("{0} don't serialize fields", errorMissedFields);
}

static void Main(string[] args)
{

ExceptionTester tester = new ExceptionTester();

DirectoryInfo dirInfo = new DirectoryInfo(".");
foreach (FileInfo file in dirInfo.GetFiles("*.dll"))
{
tester.ProcessAssembly(file);
}
tester.PrintErrors();
}

}
}
--
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" <he***@myrealbox.com> wrote in message
news:uf****************@TK2MSFTNGP09.phx.gbl...
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

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pekka Niiranen | last post by:
Hi, I have Perl code looping thru lines in the file: line: while (<INFILE>) { ... $_ = do something ... if (/#START/) { # Start inner loop
5
by: HL | last post by:
Hi, I need to enumerate windows and find the sum of the rect of all the windows of a specific application. In C++, I use the APIs - 'EnumWindows , GetWindowRect and UnionRect to accomplish the...
1
by: smichr | last post by:
I see that there is a thread of a similar topic that was posted recently ( enumerate with a start index ) but thought I would start a new thread since what I am suggesting is a little different. ...
6
by: Gregory Petrosyan | last post by:
Hello! I have a question for the developer of enumerate(). Consider the following code: for x,y in coords(dots): print x, y When I want to iterate over enumerated sequence I expect this to...
2
by: eight02645999 | last post by:
hi, i am using python 2.1. Can i use the code below to simulate the enumerate() function in 2.3? If not, how to simulate in 2.1? thanks from __future__ import generators def...
8
by: Dustan | last post by:
Can I make enumerate(myObject) act differently? class A(object): def __getitem__(self, item): if item 0: return self.sequence elif item < 0: return self.sequence elif item == 0: raise...
21
by: James Stroud | last post by:
I think that it would be handy for enumerate to behave as such: def enumerate(itrbl, start=0, step=1): i = start for it in itrbl: yield (i, it) i += step This allows much more flexibility...
12
by: Danny Colligan | last post by:
In the following code snippet, I attempt to assign 10 to every index in the list a and fail because when I try to assign number to 10, number is a deep copy of the ith index (is this statement...
8
by: eight02645999 | last post by:
hi say i want to enumerate lines of a file eg for n,l in enumerate(open("file")): # print next line ie is there a way to print out the next line from current line using the above?. Or do i...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.