我希望能够使用单个 using 子句为整个应用程序设置数据类型。我的解决方案包含一堆类,每个类都在自己的.cs文件中,每个类都包含以下语句:
using ptype = System.Double; // or System.Decimal, etc
通过将后续变量声明为 ptype ,我可以通过更改这一行代码,从十进制,双精度,浮点数的计算方式进行更改。类型转换同样是自动的:
e.Position_full_mid[i_row] = (ptype)((decimal)this_Position + ((decimal)d_Position / 2M));
在这种情况下,必须以小数精度执行除法运算,然后将商显式转换为 ptype 。拥有这一控制点很好,但是 using 子句仅对其所驻留的文件具有作用域(即使它是在类外部声明的)。它不会出现在其他文件的相同名称空间中。我每次都要声明。
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace procC
{
using ptype = System.Double;
class Commutation_Proc
{
[System.Serializable]
public class input_data_val // magnetics data parsed from the FEMM results datafiles
{
public int Positions_Count_repeat = 0; //total number of positions in the range equaling <json>PositionStart to equaling <json>PositionRepeat.
public int Positions_Count_unique = 0; //total number of positions in the range equaling <json>PositionStart through less than <json>PositionRepeat. (Positions_Count_repeat - 1)
public ptype[] Position_raw, Position_full; //[Position Index] = double, containing position of the force-measured "moving group", where Position Index is "r_index" and the value is "x_mm" or "r_deg"
public ptype[,,,,] Current_raw, Current_full; //[File index, Position Index, Current / Phase Index, Winding Sector, Winding Series] = double. For Commutated data, use Phase index; this resolves down rows where "phase_idx" is index of winding polarization pattern phase rotation
到目前为止,我还没有在解决方案级别找到任何可以定义 ptype 的地方,因此更改表示形式(例如,从Double转换为Decimal)意味着编辑
using ptype = System.<whatever>;
十几课的陈述(并在计数)。可以全局定义吗?