使用T型参数调用通用方法 [英] Calling a generic method with T type parameter
问题描述
我正在尝试构建一种方法,它将采用各种数字类型并对其进行预处理以获得第二种方法。我不确定是否应该简单地重载或使用通用方法。我尝试使用泛型方法,但该方法似乎无法识别参数类型。代码如下。有人可以向我解释在这种情况下重载还是使用通用方法更好?另外,如果我想用一个通用的方法来做到这一点,我怎么才能使它工作?
public static class math
{
public static int nextpow2< T>( T a)
{
double w;
if(a.GetType()is sbyte ||
a.GetType()is byte ||
a.GetType()short ||
a.GetType()是ushort ||
a.GetType()是int ||
a.GetType()是uint ||
a.GetType()是long ||
a.GetType ()是ulong ||
a.GetType()是float ||
a.GetType()是double ||
a.GetType()是十进制
)w = (double)Convert.ChangeType(a,typeof(double));
else
抛出新的System.ArgumentException(nextpow2中的内部错误:参数a不是数字!);
return _nextpow2(w);
private static int _nextpow2(double a)
{
double index = Math.Abs(a);
int p =(index> 1)? (int)Math.Ceiling(Math.Log(index,2.0)):0;
return p;
}
我打电话给方法如下:
int indx = 0;
int p = math.nextpow2(indx);
代码无法编译。我得到以下错误:
nextpow2中的内部错误:参数a不是数字!
请解释我做错了什么?谢谢。
您可以说
a是sbyte
或说
a.GetType()== typeof(sbyte)
,但将它混入 a.GetType()为sbyte
是没有意义的。你应该得到一个编译器警告!对于 a.GetType()
将是一个从 System.Type
(实际上它会是 System.RuntimeType
),因此永远不会从 sbyte
(因为 sbyte
被封为结构体,而 sbyte
本身只从> ValueType
和对象)。
比一般方法更好的解决方案是:
public static int Nextpow2(double a)
{
...
}
$ b 除小数
以外的所有数类型都可以隐式转换为 double
。因此,通过上面的签名,您可以按照您的方式使用它:
int indx = 0;
int p = math.Nextpow2(indx);
I am trying to build a method that will take a variety of numeric types and preprocess them for a second method. I am not sure if I should be simply overloading or using a generic method. I tried to use a generic method but the method does not seem to recognize the parameter type. The code is below. Can someone please explain to me whether it is better to overload or use a generic method in this case? Also, if I wanted to use a generic method to do this, how could I make it work? Thank you very much.
public static class math
{
public static int nextpow2<T>(T a)
{
double w;
if ( a.GetType() is sbyte ||
a.GetType() is byte ||
a.GetType() is short ||
a.GetType() is ushort ||
a.GetType() is int ||
a.GetType() is uint ||
a.GetType() is long ||
a.GetType() is ulong ||
a.GetType() is float ||
a.GetType() is double ||
a.GetType() is decimal
) w = (double)Convert.ChangeType(a, typeof(double));
else
throw new System.ArgumentException("Internal error in nextpow2: argument a is not a number!");
return _nextpow2(w);
}
private static int _nextpow2(double a)
{
double index = Math.Abs(a);
int p = (index > 1) ? (int)Math.Ceiling( Math.Log( index, 2.0) ) : 0;
return p;
}
I am calling the method as follows:
int indx = 0;
int p = math.nextpow2(indx);
The code fails to compile. I get the following error:
Internal error in nextpow2: argument a is not a number!
Can someone please explain what I am doing wrong? Thank you.
You can either say
a is sbyte
or say
a.GetType() == typeof(sbyte)
but it makes no sense to mix it into a.GetType() is sbyte
. You should get a compiler warning about that! For a.GetType()
will be an object which derives from System.Type
(actually it will be System.RuntimeType
), and therefore it can never derive from sbyte
(since sbyte
is sealed, being a struct, and sbyte
itself derives only from ValueType
and Object
).
A better solution than a generic method, is to have:
public static int Nextpow2(double a)
{
...
}
All numereric types except decimal
are implicitly convertible to double
. So with the above signature, you can use it the way you want:
int indx = 0;
int p = math.Nextpow2(indx);
这篇关于使用T型参数调用通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!