473,387 Members | 1,621 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.

Problems with factory through reflection

Rob
Hello,

I'm new to C# and have a question regarding factoring through
reflection.

I have a database filled with data represented by name and value pairs.
Each name has a identically named class in my program. Each of these
classes is a descendant of a 'DataType' class. After reading about
reflection, I though I could use this naming in my advantage:

namespace Something
{
// base class of all types for the factory
public abstract class DataType
{
public abstract string Value { get; set; }
public abstract bool TryParse(string a);
};

public static class TypeFactory
{
static TypeFactory()
{
// Create a list of creatable types.
foreach (Module m in
Assembly.GetExecutingAssembly().GetModules())
foreach (Type t in m.GetTypes())
if ( (!t.IsAbstract) &&
(t.IsSubclassOf(typeof(DataType))))
TypeList.Add(t.Name.ToUpper(), t);
}

// Create the DataType by name
public static DataType CreateByName(string aName)
{
Type d = TypeList[aName.ToUpper()];
return (d==null)? null : (DataType)d.GetConstructor(
System.Type.EmptyTypes).Invoke(null);
}

public static SortedList<string, Type> TypeList =
new SortedList<string,Type>();
}
}

This works fine, but the static constructor must iterate through all
types in the assembly, of which most are not DataType descendants. This
problem will grow as the program grows.

I know reflection is not the fastest way, but most alternatives I tried
resulted in a lot of repetetive programming, since the number of
DataType classes is large.

Does anybody know a more efficient way to extract the DataType
descendents from the assembly?

Rob

Mar 26 '06 #1
0 1164

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

Similar topics

2
by: Nancy | last post by:
I have the following situation and could use some advise. I have a Base Class CreditReport, classes BoatCreditReport and HomeCreditReport Inherit from CreditReport. I would like to have the...
2
by: mag31 | last post by:
This is a technically challenging question but it would help me significantly if anyone can answer it. In essence I have the following: A factory class, which can create producer classes which...
0
by: JDR | last post by:
I am trying to implement an abstract query factory in .NET. My solution uses a Factory that creates a concrete command class using reflection and caches an instance of it in a HashTable. Each...
8
by: Leicester B. Ford Jr. | last post by:
I have come across a problem using generics. I want to create a Factory type that will build classes for me. Code snippet below... static public class Product<T> where T : new() { static...
10
by: Mark | last post by:
I have an abstract class, and a set of classes that inherit from my abstract class. The fact that it is abstract is likely irrelevant. I have a static factory method in my abstract class that...
8
by: Jason MacKenzie | last post by:
Is there a way to prevent classes from being instantiated by methods other than my factory pattern? I have a couple of classes and want to force the factory class to be used as the "entry...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
6
by: GroupReader | last post by:
In my app, I have two very similar static classes. After long thought, I've decided *yes - keep them static*. - Sometimes I will want to use Static Class A, and somtimes I will want to use...
5
by: Agrona | last post by:
I found this thread: http://www.thescripts.com/forum/thread233505.html only marginally helpful. It seems that going through the callstack is the only way to determine which function may have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.