Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
GeneralRe: Implementation of Generic Method in List Pin
Luc Pattyn26-Jun-12 12:13
sitebuilderLuc Pattyn26-Jun-12 12:13 
GeneralRe: Implementation of Generic Method in List Pin
ezazazel26-Jun-12 19:52
ezazazel26-Jun-12 19:52 
GeneralRe: Implementation of Generic Method in List Pin
Trak4Net26-Jun-12 20:31
Trak4Net26-Jun-12 20:31 
AnswerRe: Implementation of Generic Method in List Pin
DaveyM6926-Jun-12 20:08
professionalDaveyM6926-Jun-12 20:08 
GeneralRe: Implementation of Generic Method in List Pin
Trak4Net26-Jun-12 20:40
Trak4Net26-Jun-12 20:40 
GeneralRe: Implementation of Generic Method in List Pin
DaveyM6927-Jun-12 4:54
professionalDaveyM6927-Jun-12 4:54 
AnswerRe: Implementation of Generic Method in List Pin
Trak4Net26-Jun-12 21:03
Trak4Net26-Jun-12 21:03 
AnswerRe: Implementation of Generic Method in List Pin
Trak4Net27-Jun-12 1:44
Trak4Net27-Jun-12 1:44 
I am having way to much fun now...

This is the closest I could come up with to what you want and somewhat providing a safe implementation of it. Obviously you would need to implement some checks to avoid crashing if incorrect values are used and I don't know exactly what kind of calculations you are performing but here is what I got.

C#
public override T Calculate<T, P>(params P[] input) where T : IConvertible where P : IConvertible  // !! public override double Calculate(params int[] input)
{
    TypeCode mode = Type.GetTypeCode(typeof(P));

    switch (mode)
    {
        case TypeCode.Double:
            //do your calculations for double[]

                    //change array to usable format - we know T is double so we can cast them
                    double[] args = (double[])Convert.ChangeType(input, typeof(double[])); 

                    //do some math...
                    double r = 0;
                    //obviously you want to verify 
                    r = args[0] + args[1];

                    return (T)Convert.ChangeType(r, typeof(T));
        case TypeCode.Int32:
            //do your calculations for int32[]

        case TypeCode.Decimal:
            //do your calculations for decimal[]

        default:
            break;
    }

    throw new NotSupportedException("Generic type value [" + typeof(T).ToString() + "] is not supported.");
}


My test usage:

XML
double y = Calculate<int, double>(new double[] { 1.5, 2.25 });

               Console.WriteLine(y.ToString());

               try
               {
                   Calculate<string, string>(new string[] { "no", "gonna error!" });
               }
               catch (NotSupportedException ex)
               {
                   System.Diagnostics.Trace.TraceError(ex.Message);
               }


The trace output from throwing in a string value

Quote:
UnitTest.vshost.exe Error: 0 : Generic type value [System.String] is not supported.

GeneralRe: Implementation of Generic Method in List Pin
ezazazel28-Jun-12 8:11
ezazazel28-Jun-12 8:11 
QuestionRegistering COM+ Assembly Error Pin
Member 813710526-Jun-12 9:37
Member 813710526-Jun-12 9:37 
AnswerRe: Registering COM+ Assembly Error Pin
Richard Andrew x6426-Jun-12 11:48
professionalRichard Andrew x6426-Jun-12 11:48 
GeneralRe: Registering COM+ Assembly Error Pin
Member 81371056-Jul-12 5:16
Member 81371056-Jul-12 5:16 
AnswerRe: Registering COM+ Assembly Error Pin
Member 813710513-Jul-12 3:47
Member 813710513-Jul-12 3:47 
GeneralSql Update Pin
Member 771897926-Jun-12 8:53
Member 771897926-Jun-12 8:53 
GeneralRe: Sql Update Pin
PIEBALDconsult26-Jun-12 9:25
mvePIEBALDconsult26-Jun-12 9:25 
QuestionHow to check for a driver in C#? Pin
glennPattonWork326-Jun-12 3:47
professionalglennPattonWork326-Jun-12 3:47 
AnswerRe: How to check for a driver in C#? Pin
Alan N26-Jun-12 5:49
Alan N26-Jun-12 5:49 
GeneralRe: How to check for a driver in C#? Pin
glennPattonWork326-Jun-12 5:58
professionalglennPattonWork326-Jun-12 5:58 
GeneralRe: How to check for a driver in C#? Pin
Alan N26-Jun-12 6:44
Alan N26-Jun-12 6:44 
GeneralRe: How to check for a driver in C#? Pin
glennPattonWork326-Jun-12 22:21
professionalglennPattonWork326-Jun-12 22:21 
Questionkeycode Pin
a.fatemeh26-Jun-12 1:16
a.fatemeh26-Jun-12 1:16 
QuestionHow to retrieve a list of active computers in a LAN? Pin
asdf900925-Jun-12 23:47
asdf900925-Jun-12 23:47 
AnswerRe: How to retrieve a list of active computers in a LAN? Pin
Sandeep Mewara26-Jun-12 0:49
mveSandeep Mewara26-Jun-12 0:49 
QuestionSearch String for SQL Server Pin
FredS225-Jun-12 21:40
FredS225-Jun-12 21:40 
AnswerRe: Search String for SQL Server Pin
Dave Kreskowiak26-Jun-12 2:08
mveDave Kreskowiak26-Jun-12 2:08 

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.