Code: Impossible

Posts Tagged ‘.NET’

Microsoft .Net Framework Public Classes Data Dump

Tuesday, December 8th, 2009

I’ve wrapped up work on v0.4 of the Jsoq Console, and the insanely strenuous release cycle for v0.1 of WhatDidIJustEat.com so I’m starting another side project tonight.

Ever been working with a programming language you’re not 100% familiar with and find yourself wondering:

Is this function a built-in function or in some included library?
What assembly is this class in again?
Can I name my class XXXX without conflicting with another class?

I’ve had these questions recently and found myself annoyed and frustrated to no end (PHP, I’m looking at you) so I’ve decided to build a system to keep track of this stuff for me :D.

The first thing on my list (because it was the easiest) was to do a data dump of all the public classes in the .Net Framework, including the ones in my GAC, and store some metadata for each one in a database table. I’ve just finished this step and thought that this data might be useful, so I’m posting it here.

Currently the fields included in the data dump are:


[namespace] = The namespace that the class exists in (ex: System.Collections.Generic)
[class_name] = The name of the class (ex: StringBuilder)
[assembly_fullname] = The display name of the assembly (ex: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
[assembly_file] = The full file path to the assembly (ex: C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll)
[framework_name] = The framework that uses this class, for this dump they will all be 'Microsoft .Net'
[framework_version] = The framework version that uses this class (ex: v.2.0.50727)

The .sql file takes about 18 seconds to run to completion on my AMD Athlon X2 2.53ghz machine with 4gb of RAM.

If you’re curious about how I generated the .sql file, below is the code I used to find all the classes. It’s not pretty but then again it was just meant to get the data into the database. Just paste the code into a new console app and run it in a command window like so:

[application_name].exe > c:\classes.sql

Or, you can download the .sql file I’ve generated (it will also create the table you need to store the data).


static void Main(string[] args)
{
	var dictionary = new Dictionary(){
		{ "v2.0.50727", @"C:\Windows\Microsoft.NET\Framework\v2.0.50727" },
		{ "v3.0", @"C:\Windows\Microsoft.NET\Framework\v3.0" },
		{ "v3.5", @"C:\Windows\Microsoft.NET\Framework\v3.5" },
		{ "v???", @"c:\windows\assembly\gac" }
	};

	var types = new List();

	foreach (var pair in dictionary)
	{
		var path = pair.Value;

		var assemblies = Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories);

		foreach (string file in assemblies)
		{
			try
			{
				Assembly asm = Assembly.LoadFile(file);

				foreach (Type t in asm.GetTypes())
				{
					if (t.IsPublic)
					{
						Console.WriteLine(String.Format(
							"INSERT INTO Classes (" +
								"[namespace], " +
								"class_name, " +
								"assembly_fullname, " +
								"assembly_file, " +
								"framework_name, " +
								"framework_version" +
							") " +
							"VALUES (" +
								"'{0}', " +
								"'{1}', " +
								"'{2}', " +
								"'{3}', " +
								"'Microsoft .Net', " +
								"'{4}'" +
							")",
							String.IsNullOrEmpty(t.Namespace) ?
								"GLOBAL" :
								t.Namespace,
							t.Name
								.Replace("`1", "")
								.Replace("`2", "")
								.Replace("`3", ""),
							t.Assembly.FullName,
							t.Assembly.Location,
							pair.Key == "v???" ?
								t.Assembly.GetName().Version.ToString() :
								pair.Key
						));
					}
				}
			}
			catch { }
		}
	}
}

If you found this useful, let me know in the comments!

Jquery + MVC = Web Dev Heaven

Thursday, October 2nd, 2008

So Jquery is going to become part of Asp.net MVC. First off, congrats to the Jquery team. They’ve put out a really awesome product. Second, congrats to Microsoft for catching every .Net developer completely by surprise, proving, again, that they are listening to the community.

I never really got into the microsoft javascript libraries that shipped with the ajax control toolkit because I found that I could do things much faster using external javascript libraries .

It will be interesting to see what Microsoft contributes back to the Jquery community in later updates to the ASP MVC product.

Prototype to C#: Try.These()

Wednesday, October 1st, 2008

The prototype javascript library has a class (Try) and function called these(). This function accepts an array of functions as it’s sole argument and it will execute each function in the array, in the order they are added, and return the result from the first function that executes without error. If none of the functions executes successfully undefined is returned.

For example:


var i = Try.These(
    function() { return 9 / 0; },
    function() { return 1; }
);

If we ran the sample above i would equal 1 because the first function would encounter a division by zero error. However:


var h = 0;
var i = Try.These(
    function() { return 9 / h; },
    function() { return 1 / h; }
);

i will equal undefined in this example.

With the .Net framework’s generics library we can achieve roughly the same results. We won’t be able to assign an undefined value to the result but we can play around with the default keyword. :D


public class Try
{
    public static T These(params Func[] delegates)
    {
        for (int i = 0; i <= delegates.Length - 1; i++)
        {
            try
            {
                return (T)delegates[i]();
            }
            catch
            {

            }
        }
        return default(T);
    }
}

So for example:


static void Main(string[] args)
{
    int i = 0;
    int y = Try.These(
        delegate() {
            int x = 0;
            return i / x;
        },

        delegate() {
            return ++i;
    });

    Console.Write("y is " + y.ToString());
}

Will output

y is 0

Enumerate windows users' groups

Tuesday, April 8th, 2008

The code below will enumerate which groups a domain / local user belongs to.

[sourcecode language="csharp"]

private static string GetUserGroups(string strUser)
{
string groups = “”;
DirectoryEntry de = null;
try
{

string entryName = String.Format(“WinNT://{0},user”,
strUser.Replace(“\\”, “/”));

de = new DirectoryEntry(entryName);

object oGroups = de.Invoke(“Groups”);
foreach (object o in (IEnumerable)oGroups)
{
DirectoryEntry group =
new DirectoryEntry(oGroups);
groups += group.Name + “,”;
}
}
catch (Exception)
{
throw;
}
return groups;
}

[/sourcecode]

C# Extension Methods

Thursday, March 20th, 2008

So over the last few days I’ve been screwing around with C# extension methods in Visual C# 2008 Express and I have to say they are fast becoming my favorite addition to the .NET framework. What started me on extensions was the fact that in Perl you can do the following with string manipulation:

(more…)


Page optimized by WP Minify WordPress Plugin