C#泛型与类型的细微差别? [英] c# generic with slight difference for types?
问题描述
注意两个扩展,一个用于浮动,一为的Vector3。
Notice the two extensions, one for float, one for Vector3.
注意有只在 VAR略有差别(
电话。
在C#中可以将这些写成一个作为通用??
In c# could these be written as one as a generic??
我的问题的实质是:
<中的通用EM>,可你的类型的性质分支
within a generic, can you branch on the nature of the type?
public static IEnumerator Tweeng( this float duration,
System.Action<float> vary, float aa, float zz )
{
float sT = Time.time;
float eT = sT + duration;
while (Time.time < eT)
{
float t = (Time.time-sT)/duration;
vary( Mathf.SmoothStep(aa,zz, t) ); // slight difference here
yield return null;
}
vary(zz);
}
public static IEnumerator Tweeng( this float duration,
System.Action<Vector3> vary, Vector3 aa, Vector3 zz )
{
float sT = Time.time;
float eT = sT + duration;
while (Time.time < eT)
{
float t = (Time.time-sT)/duration;
vary( Vector3.Lerp(aa,zz, t) ); // slight difference here
yield return null;
}
vary(zz);
}
的(BTW任何C#大师阅读,代码例子是团结,在那里你一个协程访问框架系统。)的
有关的任何统一的开发者阅读,您怎么称呼Tweeng
For any Unity devs reading, examples of how you call Tweeng
// tweeng z to 20 degrees in .12 seconds
StartCoroutine(.12f.Tweeng( (t)=>transform.Eulers(0f,0f,t), 0f,20f) );
// fade in alpha in .75 seconds
StartCoroutine(.75f.Tweeng( (u)=>{c.a=u;s.color=c;}, 0f,1f) );
(如果你是新来团结和不熟悉的扩展的基本概念,这里是一个< A HREF =http://stackoverflow.com/a/35629303/294884>介绍。)
(If you're new to Unity and not familiar with the basic concept of extensions, here's an intro.)
推荐答案
你可以做,如果你做一个额外的 Func键< T,T>
在调用 VAR之前要进行改造
行动(你应该重新命名,因为 VAR
是一个C#关键字)。
You can do it if you make an extra Func<T,T>
that performs transformation before calling the var
action (which you should rename, because var
is a C# keyword).
下面是一个方法,你可以采取
Here is one approach that you could take:
public static IEnumerator Tweeng<T>(
this float duration
, System.Action<T> varAction
, T aa
, T zz
) {
Func<T,T,float,T> transform = MakeTransform<T>();
float sT = Time.time;
float eT = sT + duration;
while (Time.time < eT) {
float t = (Time.time-sT)/duration;
varAction(transform(aa, zz, t));
yield return null;
}
varAction(zz);
}
private static Func<T,T,float,T> MakeTransform<T>() {
if (typeof(T) == typeof(float)) {
Func<float, float, float, float> f = Mathf.SmoothStep;
return (Func<T,T,float,T>)(Delegate)f;
}
if (typeof(T) == typeof(Vector3)) {
Func<Vector3, Vector3, float, Vector3> f = Vector3.Lerp;
return (Func<T,T,float,T>)(Delegate)f;
}
throw new ArgumentException("Unexpected type "+typeof(T));
}
它甚至可以在线完成:
It can even be done inline:
public static IEnumerator DasTweeng<T>( this float duration, System.Action<T> vary, T aa, T zz )
{
float sT = Time.time;
float eT = sT + duration;
Func<T,T,float,T> step;
if (typeof(T) == typeof(float))
step = (Func<T,T,float,T>)(Delegate)(Func<float, float, float, float>)Mathf.SmoothStep;
else if (typeof(T) == typeof(Vector3))
step = (Func<T,T,float,T>)(Delegate)(Func<Vector3, Vector3, float, Vector3>)Vector3.Lerp;
else
throw new ArgumentException("Unexpected type "+typeof(T));
while (Time.time < eT)
{
float t = (Time.time-sT)/duration;
vary( step(aa,zz, t) );
yield return null;
}
vary(zz);
}
也许一个更自然的习惯用法
Perhaps a more natural idiom is
Delegate d;
if (typeof(T) == typeof(float))
d = (Func<float, float, float, float>)Mathf.SmoothStep;
else if (typeof(T) == typeof(Vector3))
d = (Func<Vector3, Vector3, float, Vector3>)Vector3.Lerp;
else
throw new ArgumentException("Unexpected type "+typeof(T));
Func<T,T,float,T> step = (Func<T,T,float,T>)d;
这篇关于C#泛型与类型的细微差别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!