المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Design and specific code suggestions for reflection and access base class



C# Programming
01-30-2013, 02:10 AM
I'm refactoring some code, and I'm trying to figure out if it's best to to access a base class method using reflection, or if I should access the child class. I definitely need to figure out how to access the base class to set up my form with data (see GetProgControl below). Does anyone know how to do this? I should probably be accessing a method in the child class to access the method in the parent class from the child class, since I am refactoring so I can add some child-specific methods later. The parent class will be common across several projects and classes. The child class will be pushing some group box and comboBox info up to the parent's form, with data. The main form is the base class' form, which I get the control of using GetProgControl using reflection below; however this isn't working since I used to do this when the parent class was what I was accessing directly.

I've looked at the following links:
http://stackoverflow.com/questions/12051/calling-base-constructor-in-c-sharp[^ (http://stackoverflow.com/questions/12051/calling-base-constructor-in-c-sharp)]

http://stackoverflow.com/questions/5558868/printout-the-method-name-of-a-derived-class-by-calling-method-of-a-base-class[^ (http://stackoverflow.com/questions/5558868/printout-the-method-name-of-a-derived-class-by-calling-method-of-a-base-class)]

http://stackoverflow.com/questions/2267277/get-private-properties-method-of-base-class-with-reflection[^ (http://stackoverflow.com/questions/2267277/get-private-properties-method-of-base-class-with-reflection)]

My code, which is still set up to access the base class from before instead of the child class, looks like this:

frmUseFrm.cs:
//in preparation to add tabs to Generic's form (worked fine before generic became parent class)
if (controls[i] == null){ try { _assembly = Assembly.LoadFrom(programDll); _type = _assembly.GetType("CrWr.ProgControl"); //problem here? _objectInstance = Activator.CreateInstance(_type); _parameters = new Object[] { params }; controls[i] = new Control(); controls[i] = (Control)_type.InvokeMember("GetProgControl", BindingFlags.Default | BindingFlags.InvokeMethod, null, _objectInstance, _parameters); controls[i].Dock = DockStyle.None; this.Controls.Add(controls[i]); }}

My child class, El.cs, looks like this:
using GenericCrWr; namespace CrWr{ public partial class ProgControl : GenericProgControl { public Control GetProgControl(...) { return base(GetProgControl(...)); //this doesn't compile...(Only assignment, call, increment, dec, and new object expressions can be used as a statement; use of keyword 'base' is not valid in this context;a local var named "x' cannot be declared in this scope because it would give a different meaning to x, which is already used in a parent or current scope to denote something else) } }}
The parent/base class, Generic.cs, looks like this:

namespace GenericCrWr{ public partial class GenericProgControl : UserControl { //class variables //constructor public GenericProgControl() {...} public Control GetProgControl(...) {...} }}