Google
 
Web www.mndtechnologies.com

Wednesday, April 26, 2006

Finding classes derived from base class/interface

I was working on an application for last few days. This is a very big application in which most of the components are loose coupled. There are a number of base classes and interfaces on which each component of the application works.

While testing the application's controls/components, I need to know the class of which I can create an object, as the component I works on has base class type reference or an interface reference, e.g.

private string [] GetDataArray(System.Data.IdbCommand command, string fieldName)
{
System.Collections.ArrayList al = new System.Collections.ArrayList();

System.Data.IDataReader dr = command.ExecuteReader();
while(dr.Read())
al.Add(dr.GetString(dr.GetOrdinal(fieldName)));

dr.Close();

string [] sArr = new string [al.Count];

int i = 0;
foreach(string s in al)
sArr[i++] = s;

return sArr;
}

The code above receives a IdbCommand type object and executes reader on that. After that read data from the reader for the field specified, and returns the string array.

So, I am not able to create a concrete object unless I know the classes derived from the base interface or the classes derived from the base class. Here I know that SqlCommand is derived from IdbCommand so I can create an object of that type and test the method GetDataArray. What if I say that the argument passed to the method is of type IMyInterface. I will not be able to test the method unless I have a concrete object of a class that implements the interface IMyInterface.

Now, I need to know all the classes derived from the base interface/class.

The first idea I got was to write a parser that will parse all the projects in the solution and their references to fetch the base class of each and every class. This idea was dropped because it will take a lot of time and efforts.

So, I came down to a quick solution. I change the base class's/interface's name and compiled the project.
I know I know, the project will break down. But the error list gave me the classes that were deriving from the base class/interface.

Huh, finally, I got a class to create a concrete object and test the methods.

Saturday, April 22, 2006

First Post of this blog

Hey guyz,

I am Manish Dalal, created this blog to blog, he he.

I am 26M, India, working on .NET 1.1.

Its been 4 tough years that I am working on software development.

cerebral palsy
cerebral palsy