Click here to Skip to main content
16,015,003 members
Home / Discussions / C#
   

C#

 
QuestionShould I be using inhertance for this? Pin
Jacob D Dixon4-Dec-10 15:26
Jacob D Dixon4-Dec-10 15:26 
AnswerRe: Should I be using inhertance for this? Pin
PIEBALDconsult4-Dec-10 16:30
mvePIEBALDconsult4-Dec-10 16:30 
GeneralRe: Should I be using inhertance for this? [modified] Pin
Jacob D Dixon4-Dec-10 17:14
Jacob D Dixon4-Dec-10 17:14 
GeneralRe: Should I be using inhertance for this? Pin
PIEBALDconsult5-Dec-10 3:49
mvePIEBALDconsult5-Dec-10 3:49 
GeneralRe: Should I be using inhertance for this? Pin
Jacob D Dixon5-Dec-10 4:15
Jacob D Dixon5-Dec-10 4:15 
GeneralRe: Should I be using inhertance for this? Pin
PIEBALDconsult5-Dec-10 8:53
mvePIEBALDconsult5-Dec-10 8:53 
GeneralRe: Should I be using inhertance for this? Pin
Jacob D Dixon6-Dec-10 16:17
Jacob D Dixon6-Dec-10 16:17 
GeneralRe: Should I be using inhertance for this? Pin
Spectre_0016-Dec-10 2:33
Spectre_0016-Dec-10 2:33 
Have your client code use reflection and generics to dynamically invoke your object's methods:

<br />
#region Libraries<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Reflection;<br />
#endregion<br />
<br />
namespace Utilities<br />
{<br />
    // T is the type of the object containing the method to be invoked.<br />
    // R is the type of the return value expected.<br />
    // Methods that fail to return a value, or are declared as void,<br />
    // will return default(R), which in many cases (depending on typeof(R)) will be null.<br />
    public class DynamicMethodInvocation<T, R><br />
    {<br />
        // The methodParameterValues dictionary requires the parameter's name as the key<br />
        // and the parameter's value as the object. If ParameterValues == null, no<br />
        // parameters are passed to the method invocation.<br />
        public R Invoke(string MethodName, IDictionary<string, object> methodParameterValues)<br />
        {<br />
            return Invoke(MethodName, methodParameterValues, null);<br />
        }<br />
<br />
        // constructorParameters are the parameters required by the object T's constructor<br />
        // if any. If T's constructor requires no parameters pass null, or use the<br />
        // overload.<br />
        public R Invoke(string MethodName, IDictionary<string, object> methodParameterValues, object[] constructorParameters)<br />
        {<br />
            T instance = (T)Activator.CreateInstance(typeof(T), constructorParameters);<br />
            MethodInfo mi = instance.GetType().GetMethod(MethodName);<br />
            if (methodParameterValues != null)<br />
            {<br />
                ParameterInfo[] parameters = mi.GetParameters();<br />
                object[] values = (object[])Array.CreateInstance(typeof(object), parameters.Length);<br />
                foreach (ParameterInfo parameter in parameters)<br />
                {<br />
                    values[parameter.Position] = methodParameterValues[parameter.Name];<br />
                }<br />
                return (R)mi.Invoke(instance, values);<br />
            }<br />
            else<br />
            {<br />
                return (R)mi.Invoke(instance, null);<br />
            }<br />
        }<br />
    }<br />
}<br />

Kevin Rucker, Application Programmer
QSS Group, Inc.
United States Coast Guard OSC
Kevin.D.Rucker@uscg.mil

"Programming is an art form that fights back." -- Chad Hower

AnswerRe: Should I be using inhertance for this? Pin
nortee5-Dec-10 21:29
nortee5-Dec-10 21:29 
AnswerRe: Should I be using inhertance for this? Pin
Steve Naidamast6-Dec-10 3:04
professionalSteve Naidamast6-Dec-10 3:04 
AnswerRe: Should I be using inhertance for this? Pin
mbb016-Dec-10 22:05
mbb016-Dec-10 22:05 
GeneralRe: Should I be using inhertance for this? Pin
nortee6-Dec-10 23:21
nortee6-Dec-10 23:21 
GeneralRe: Should I be using inhertance for this? Pin
Jacob D Dixon8-Dec-10 3:46
Jacob D Dixon8-Dec-10 3:46 
AnswerRe: Should I be using inhertance for this? Pin
James Lonero8-Dec-10 8:41
James Lonero8-Dec-10 8:41 
QuestionMessage Removed Pin
4-Dec-10 13:27
SRJ924-Dec-10 13:27 
AnswerRe: Game Creation Pin
Luc Pattyn4-Dec-10 13:56
sitebuilderLuc Pattyn4-Dec-10 13:56 
GeneralRe: Game Creation Pin
SRJ924-Dec-10 14:09
SRJ924-Dec-10 14:09 
GeneralRe: Game Creation Pin
Luc Pattyn4-Dec-10 14:16
sitebuilderLuc Pattyn4-Dec-10 14:16 
GeneralRe: Game Creation Pin
SRJ924-Dec-10 14:21
SRJ924-Dec-10 14:21 
GeneralRe: Game Creation Pin
Pete O'Hanlon5-Dec-10 9:33
mvePete O'Hanlon5-Dec-10 9:33 
GeneralRe: Game Creation Pin
Luc Pattyn5-Dec-10 13:56
sitebuilderLuc Pattyn5-Dec-10 13:56 
QuestionwindowsForms Pin
nasis14-Dec-10 2:16
nasis14-Dec-10 2:16 
AnswerRe: windowsForms Pin
Eddy Vluggen4-Dec-10 2:37
professionalEddy Vluggen4-Dec-10 2:37 
AnswerRe: windowsForms Pin
Keith Barrow4-Dec-10 5:16
professionalKeith Barrow4-Dec-10 5:16 
GeneralRe: windowsForms Pin
Not Active4-Dec-10 5:50
mentorNot Active4-Dec-10 5:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.