Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Message from discussion .NET generics & the .NET framework: not generic enough?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Daniel O'Connell [C# MVP]  
View profile  
 More options Jul 17 2004, 5:17 pm
Newsgroups: microsoft.public.dotnet.languages.csharp
From: "Daniel O'Connell [C# MVP]" <onyxk...@--NOSPAM--comcast.net>
Date: Sat, 17 Jul 2004 16:17:45 -0500
Local: Sat, Jul 17 2004 5:17 pm
Subject: Re: .NET generics & the .NET framework: not generic enough?

"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_...@msn.com> wrote in message
news:OprQenDbEHA.972@TK2MSFTNGP12.phx.gbl...

> Andreas,
>> Implementing proxies is of course always an option but I think it is a
>> really bad one.
> I sincerely hope you did not mean "really bad one" as bad as it sounds.
> :-|

> I agree its not necessarily a good option. However! In light of not having
> Operator constraints and the base types (Int32) not supporting an
> IArithmetic<T> directly, I find it a reasonable good workaround.

> I would love to hear a "better" work around, that can implemented today,
> as
> I have a couple of places where doing math generically would be useful...

> A second alternative I've wondered how usable would be Generic Delegates,
> only I'm not sure how easy they would be to use in this case.\

Well, you *could* use dynamic methods, assuming you have the permissions:
  public delegate T Operator<T>(T a,T b);
  public static Operator<T> CreateAddOperator<T>()
  {
   DynamicMethod method = new DynamicMethod("AddMethod" +
typeof(T).ToString(), typeof(T), new Type[] { typeof(T), typeof(T) },
typeof(OperatorFactory));
   ILGenerator ilg = method.GetILGenerator();
   ilg.Emit(OpCodes.Ldarg_0);
   ilg.Emit(OpCodes.Ldarg_1);
   if (typeof(T).IsPrimitive)
   {

    ilg.Emit(OpCodes.Add);

   }
   else
   {
    //do reflection on typeof(T) and generate op_Add code.
    MethodInfo info = typeof(T).GetMethod("op_Addition", new Type[] {
typeof(T), typeof(T) }, null);
    ilg.EmitCall(OpCodes.Call, info, null);
   }
   ilg.Emit(OpCodes.Ret);

   return (Operator<T>)method.CreateDelegate(typeof(Operator<T>));
  }

 I've only tested int and decimal, but it does work for those two. This
doesn't handle multiple add types(although you could write one). The biggest
problem is DynamicMethods have some permission requirements and my not be
emittable in some situations. I'm not sure how the security works with
regards to them however.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.