作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想知道是否有一种方法,在 C# 中,有一个基于基本类型的类型,并复制它以使用其他基本类型。
我知道这不是很清楚,我真的不知道如何解释这个(而且英语不是我的母语,抱歉......),所以我会尝试用伪代码来解释它.
一个简单的例子:
public struct Vector2d {
public double X;
public double Y;
//Arithmetic methods: Length, normalize, rotate, operators overloads, etc...
}
问题是我想要这种类型的 float 和 int32 版本。
我实际上在做什么(有效的 C# 代码):
//This type contains all useful methods
public struct Vector2d {
public float X;
public float Y;
public Vector2f AsFloat() {
return new Vector2f((float)X, (float)Y);
}
public Vector2f AsInteger() {
return new Vector2i((int)X, (int)Y);
}
//Arithmetic methods
}
//These types are only here to be casted from/to, all the arithmetics methods are on the double-based type
public struct Vector2f {
public float X;
public float Y;
public Vector2f AsDouble() {
return new Vector2d(X, Y);
}
public Vector2f AsInteger() {
return new Vector2i((int)X, (int)Y);
}
}
public struct Vector2i {
public int X;
public int Y;
public Vector2f AsFloat() {
return new Vector2f(X, Y);
}
public Vector2f AsDouble() {
return new Vector2d(X, Y);
}
}
虽然有效但并不“性感”,如果使用 AsXXX 方法进行大量转换,将不可避免地影响性能。
理想情况下,我会在所有三种类型上都有算术方法,但维护起来会很痛苦......
什么是理想的解决方案(伪代码,无效的 C#):
public struct Vector2<T> where T : numeric {
public T X;
public T Y;
public T Length {
return (T)Math.Sqrt(X * X + Y * Y);
}
//Other arithmetic methods
}
我知道目前这在 C# 中是不可能的,但这是真正的问题:
您是否知道如何妥善有效地处理这个问题?
我一直在想的(伪代码,无效的 C#):
//The TYPE defined constant should be used by Vector2Body instead of plain typed "double", "float", etc...
public struct Vector2d {
#define TYPE double
#import Vector2Body
}
public struct Vector2f {
#define TYPE float
#import Vector2Body
}
public struct Vector2i {
#define TYPE int
#import Vector2Body
}
这样我就不会有重复的代码,更容易维护
等待你的想法:)
PS:如果版主对如何更好地格式化我的问题有想法,请随时对其进行编辑:)
最佳答案
这是 @mike-z 给出的一个非常好的方法,使用 T4 模板:
模板(.tt 文件):
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
using System;
namespace My.Math {
<# Vector2Body("d", "double"); #>
<# Vector2Body("f", "float"); #>
<# Vector2Body("i", "int"); #>
}
<#+ private void Vector2Body(string suffix, string t) { #>
public struct Vector2<#= suffix #> {
public <#= t #> X;
public <#= t #> Y;
//Arithmetic for <#= t #>...
}
<#+ } #>
以及自动生成的代码:
using System;
namespace My.Math {
public struct Vector2d {
public double X;
public double Y;
//Arithmetic for double...
}
public struct Vector2f {
public float X;
public float Y;
//Arithmetic for float...
}
public struct Vector2i {
public int X;
public int Y;
//Arithmetic for int...
}
}
没有泛型,所以没有性能影响,代码一次编写...
关于c# - C#中泛型数值类型的讨论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17020360/
我是一名优秀的程序员,十分优秀!