maddogs

maddogs

  • NA
  • 1
  • 0

How to re-assign specific C# class type to generic object type?

Aug 19 2005 12:28 PM

I have a situation where I have three separate C# class objects that all have the same properties and methods so their signatures are the same in all respects except for the class name (I am writing a code generator for personal use with each class representing a different design pattern).
There is also a core class which operates as a control point to call each class depending on which design pattern is chosen.   The challenge I have is that it would be extremely cumbersome and complex in the control class to populate the many properties and call the many methods separately for each design pattern class so I was attempting to declare a generic object type first, instantiate the proper pattern class inside a switch block, assign the instantiated pattern class to the generic object type outside of the switch block, and finally populate all the necessary properties and call all the necessary methods using the generic object type class.
The result in many different attemtps to do this always result in the compiler returning an error message saying the generic object type class does not contain the properties or methods specifed and I am looking for way to make this happen.

A psuedo-code example of what I am trying to do is as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  // Declare generic object type
  object cNetPattern;

  // Determine design pattern requested
  switch(m_iNETDesignPattern)
  {
   case (int)eNETDesignPattern.TDSTraversal:
    // Instantiate TDS Traversal design pattern class 'clsPattern_1' here
    break;
   case (int)eNETDesignPattern.Collection:
    // Instantiate Collections design pattern class 'clsPattern_2' here  
    break;
   case (int)eNETDesignPattern.ClassProvider:
    // Instantiate Class Provider design pattern class 'clsPattern_3' here 
    break;
  }

  // Assign specific class type object to generic object type
  cNetPattern = clsPattern_n; // Where 'n' is the specific pattern class object instantiated above

  // Populate proerties and call methods
  cNetPattern.bProperty1 = m_bProperty1;
  cNetPattern.Method1();
  ...many, many more properties and methods

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

NOTE:  As mentioned all three specific design patterns have the same property and method signatures.

Any help would be grealy appreciated!