How to impose a support type of some operators in a C # type parameter?

Possible duplicate:
Define a generic type that implements the + operator .

I recently worked on a C # class library that implements an algorithm. The fact is that I would like library users to be able to choose the accuracy of the machine (single or double) with which the algorithm should work, and I'm trying to do this using generics. For example:

Algorithm<double> a = new Algorithm<double>(); /** Some initializations here */ double result = a.Solve(); 

or

  Algorithm<float> a = new Algorithm<float>(); /** Some initializations here */ float result = a.Solve(); 

Thus, the type parameter for general classes means a decimal number (because in the algorithm code I need to use +, *, /, -), but I do not know what type restriction to impose on it. I was thinking of creating an interface with all operators, but unfortunately this is not allowed. Any ideas?

Otherwise, is it possible to get in C # something similar to specializing a template in C ++?

thanks

Tommaso

+6
operators generics c # constraints
source share
4 answers

You cannot, in principle. A CLR system does not support this limitation.

Earlier, I wrote about how we can describe such restrictions using “static interfaces” , but I don’t know of any plans to do something like this.

You might want to take a look at the support of the universal MiscUtil operator , which will allow you to use operators - but you will have to use only a struct constraint or something like that, which, of course, will allow you things like Guid .

+7
source share

The closest match is a value type restriction.

C # allows only three types of constraints

  • Withdrawal limit
  • Constructor
  • Type of reference / value.

For complete documentation, contact MSDN:
http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx#csharp_generics_topic4

+1
source share

You can use a workaround: Define a method for each type that you want to support

 private static Add(float a, float b) {return a+b;} private static Add(double a, double b) {return a+b;} ... 

and create a delegate for the correct at runtime:

 MethodInfo addMethod=this.GetType().GetMethod("Add",BindingFlags.NotPublic|Static,null,new Type[2]{typeof(T),typeof(T)},null); Func<T,T,T> addFunction=(Func<T,T,T>)Delegate.CreateDelegate(typeof(Func<T,T,T>),addMethod); 
0
source share

This is usually a serious hack hack, but you can create a general “wrapper” class for numeric types that provides the operations you need and prevents the creation of an instance using an unsupported value type (i.e., char), and then specify that the general the parameter type of your algorithm should be a wrapper class.

I like the Floste workaround in every way, except for code duplication. You could make it easier; specify private method overloads, then one publicly available common method that just calls the private method, and the runtime chooses the correct one (be sure to include catch-all, which throws a NotImplementedException).

0
source share

All Articles