Can I (dis)suggest to use reflection and emittion to call op_addition
or use the add opcode from MSIL to call the correct "+" (only if it's
supported by the type... note: this can crach you app), and set in on
a try ... catch bacause even knowing it supports it... well, you know
try is light in .NET
And even if you got the Idea, please don't try, unless you are with a
responsible adult. This may be the kind of things Joda would argue you
do should not. But if you like the dark side...
Ok, know for real try to solve the problem: Use LINQ Expressions, they
can map the Addition to the correct method. (I can't help futher if
you are locked in .NET 2.0)
here some code:
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(add(12, 15));
Console.ReadKey();
}
private static T add(T a, T b)
{
Type t = typeof(T);
ParameterExpression _a = Expression.Parameter(t, "a");
ParameterExpression _b = Expression.Parameter(t, "b");
var R = Expression.Lambda>(
Expression.Add(
_a, _b
)
, _a, _b).Compile();
return R(a, b);
}
}
}
it's from my (not to well recived T_T) article
http://www.codeproject.com/KB/dotnet/HardcoreDotNET.aspx
(Ok the name was kind of hoax, or something back then). It has some
obscure magic, it may be interesting, but don't use it beyond your
learning proccess, it's dangerous, don't do it in production code, you
are warned.
Note: I haven't tested adding strings with Linq Expressions, and it's
been a long time since I used them, so I can't tell if it will do for
strings.
Here a more old school, safer way, boring if you ask me, way of doing
things:
http://www.codeproject.com/KB/cs/GenericArithmeticUtil.aspx
In this article, you will find a more pure object oriented solution,
long tested aproach, secure code based, that In my opinion makes just
worthless to implement it generic in the first place. If you are using
generics and taking this aproach you can perfectly ignore the
"generic" stuff and implement it on object and set an abstract factory
based on GetType. Even if you use generics with it, you will have to
write the addition for every type supported (not that you can't copy
paste), but it's boring. Damn that's why the dark side is so tempting.
So you say, Skywalker. Anakin or Luke?
(Darth?) Theraot
On 28 dic, 08:40, Raj <[email protected]> wrote:
> its not bug, Generics donot allow + Sign.
>
> 2009/12/28 Maddy <[email protected]>
>
>
>
>
>
> > Dear folks,
>
> > As i am working in C# Generics i found this little bit Strange
>
> > My code goes like this
> > Consider this as my Business Object
>
> > Public Class *BO*<type1,type2,type3>
> > {
> > private type1 Variable1;
> > private type2 Variable2;
> > private type3 Variable3;
>
> > public type1 val1
> > {
> > set { Variable1 = value; }
> > get { return Variable1; }
> > }
> > public type2 val2
> > {
> > set { Variable2 = value; }
> > get { return Variable2; }
> > }
> > public type3 val3
> > {
> > set { Variable3 = value; }
> > get { return Variable3; }
> > }
>
> > }
>
> > and Business Access Layer
>
> > Public Class *BAL:BO*<Object,Object,Object>//Because i don't want to
> > confine my self to work with particular Data type for this
> > //Class so i declared it as Object where if i can pass string,datetime,
> > int,etc and It ll do Boxing(data type to Object)
> > {
>
> > // take a look at this function as i am declaring a Function to Perform
> > "+" operation(for string -> Concatenation and for integer -> Sum)
> > public T getValue<T>()
> > {
> > T temp1 = (T)val1; // I am Type Casting my Object to this Type
> > T temp2 = (T)val2; // I am Type Casting my Object to this Type
> > T temp3 = temp1 + temp2; // I am Adding the values
> > return (temp3); // And returning with the Same Data type "T"
> > }
> > }
>
> > Declaring in aspx.cs as
>
> > obj.val1 = TextBox1.Text;
> > obj.val2 = TextBox2.Text;
>
> > Label1.Text = obj.getValue<string>(); // Here i am passing the generic data
> > type as String so the function will perform Concatenation
>
> > *ERROR: Operator '+' cannot be applied to operands of type 'T' and 'T'*
>
> > The Error shows (As of my Understanding) that T is Not a Valid Data type
> > TO PERFORM "+"
>
> > Clarifications required if possible:
> > 1. Then Why the Data type T can be Type casted from an Object (T temp1 =
> > (T)val1;)
> > 2. *Label1.Text *should assign to string type, as i am passing (*Label1.Text
> > = obj.getValue<int>();)* so passing type and the return type is recognized
> > as String so didn't throw any error but why it cant able to perform
> > Concatenation
>
> > *Is that A BUG From Microsoft or From My side ????*
>
> > --
> > regards
> > Sarvesh
>
> --
> Cheers,
> Rajhttp://rajmittal.blogspot.com
> Sent from Chandigarh, PB, India