reflection - C# Approach to create dynamic objects with Intellisense support -



reflection - C# Approach to create dynamic objects with Intellisense support -

i have client/server application using c#.

in design, client request server info filtering , gathering supported server through c# classes , methods.

i have single server connection receives requests , provides info client. so, client requests object, method , parameters.

so, there client request like:

getdata (myclass, mymethod, myparams[]);

it works fine i´m using dynamic method invokation @ server:

type type = system.type.gettype(myclass); object instance = system.activator.createinstance(type); methodinfo method = type.getmethod(mymethod); object[] parameters = new object[] { myparams[] }; var info = method.invoke(instance, parameters); ... info checking, filtering , returning client...

i have problem have several different objects , methods, each 1 different checking , filtering. dynamic object not back upwards intellisense, checking code after info big mess, hard back upwards , understand.

i wish have different approach that, info intellisense back upwards (known type). create life easier , code clearer. on other side, don´t build dozen of getdata calls different combinations.

i´m thinkin of building object factory, i´m not sure kind of pattern shall best suited here. i´m sure there pattern used here, can´t see solution problem...

any help much appreciated.

it's hard determine complexity of problem, without more details, think looking strategy pattern

runnable example

abstract class strategy { public abstract void algorithminterface(); } class concretestrategya : strategy { public override void algorithminterface() { console.writeline( "called concretestrategya.algorithminterface()"); } } class concretestrategyb : strategy { public override void algorithminterface() { console.writeline( "called concretestrategyb.algorithminterface()"); } } class context { private strategy _strategy; // constructor public context(strategy strategy) { this._strategy = strategy; } public void contextinterface() { _strategy.algorithminterface(); } } class mainapp { /// <summary> /// entry point console application. /// </summary> static void main() { context context; // 2 contexts next different strategies context = new context(new concretestrategya()); context.contextinterface(); context = new context(new concretestrategyb()); context.contextinterface(); // wait user console.readkey(); } }

quote design patterns: elements of reusable object-oriented software

strategy

define family of algorithms, encapsulate each one, , create them interchangeable. strategy lets algorithm vary independently clients utilize it.

applicability

use strategy pattern when:

many related classes differ in behavior. strategies provide way configure class 1 of many behaviors.

you need different variants of algorithm. example, might define algorithms reflecting different space/time trade-offs. strategies can used when these variants implemented class hierarchy of algorithms

an algorithm uses info clients shouldn't know about. utilize strategy pattern avoid exposing complex, algorithm-specific info structures.

c# reflection intellisense factory-pattern dynamicobject

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -