c# 封装成类似动态类型,类似matlab的尝试

littlebird01 2011-03-24 12:04:30
本来想发到技术区,分数不够了,发这里算了,
自己粗粗实现了一些,
大家帮着看看有没有更好的方法,

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class MO : Object
{
public MO(){}
public MO(int a)
{
v = a;
}

public MO(int[] a)
{
v = a;
}

public MO(double a)
{
v = a;
}
public MO(double[] a)
{
v = a;
}

public static implicit operator MO(int a)
{
return new MO(a);
}

public static implicit operator MO(int[] a)
{
return new MO(a);
}

public static implicit operator MO(double a)
{
return new MO(a);
}

public static implicit operator MO(double[] a)
{
return new MO(a);
}

public static implicit operator int(MO m)
{
return (int)m.v;
}

public static implicit operator int[](MO m)
{
return (int[])m.v;
}

public static implicit operator double(MO m)
{
return Convert.ToDouble(m.v);
}

public static implicit operator double[](MO m)
{
return (double[])m.v;
}

public static MO operator +(MO m, MO a)
{
int level = 0;//0 is int, 1 is float, 2 is double
if (m.v.GetType() == typeof(double) || a.v.GetType() == typeof(double)
|| m.v.GetType() == typeof(double[]) || a.v.GetType() == typeof(double[])
)
level = 2;

if (level == 0)
{
return MO.generalAdd_Int(m, a);
}

else if (level == 2)
{
double[] tmp1 = new double[] { }, tmp2 = new double[] { };

MO.castToDoubleArray(ref tmp1, m);
MO.castToDoubleArray(ref tmp2, a);
return MO.generalAdd_Double(tmp1,tmp2);
}

return new MO(0);
}

private static void castToDoubleArray(ref double[] tmp1, MO m)
{
if (m.v.GetType() == typeof(int) || m.v.GetType() == typeof(double))
tmp1 = new double[] { (double)m };
else if (m.v.GetType() == typeof(int[]))
{

tmp1 = new double[((int[])m).Length];

for (int i = 0; i < tmp1.Length; i++)
tmp1[i] = ((int[])m)[i];
}
else if (m.v.GetType() == typeof(double[]))
tmp1 =(double[])m;
}

private static MO generalAdd_Int(MO m, MO a)
{
if (m.v.GetType() == typeof(int) && a.v.GetType() == typeof(int))
return new MO((int)m + (int)a);

if (m.v.GetType() == typeof(int[]) && a.v.GetType() == typeof(int))
{
int[] tmp = (int[])m;
for (int i = 0; i < tmp.Length; i++)
tmp[i] += (int)a;

return new MO(tmp);
}

if (a.v.GetType() == typeof(int[]) && m.v.GetType() == typeof(int))
{
int[] tmp = (int[])a;
for (int i = 0; i < tmp.Length; i++)
tmp[i] += (int)m;

return new MO(tmp);
}

if (m.v.GetType() == typeof(int[]) && a.v.GetType() == typeof(int[]))
{
if (((int[])m).Length == ((int[])a).Length)
{
int[] tmp = (int[])m;
for (int i = 0; i < tmp.Length; i++)
tmp[i] += (int)a;

return new MO(tmp);
}
else
throw new Exception("int[] length should be same");
}

return new MO(0);

}

private static MO generalAdd_Double(double[] a,double[] b)
{
if (a.Length == 1)
{
for (int i = 0; i < b.Length; i++)
b[i] += a[0];
return new MO(b);
}

if (b.Length == 1)
{
for (int i = 0; i < a.Length; i++)
a[i] += b[0];
return new MO(a);
}

if (a.Length == b.Length)
{
for (int i = 0; i < b.Length; i++)
a[i] += b[i];
return new MO(a);
}
else
throw new Exception("size should be same");

return new MO(0);

}

object _value;
public object v
{
get
{
if (_value == null)
_value = new object();
return _value;
}
set
{
_value = value;
}
}
}

class Program
{
static void Main(string[] args)
{
MO a = 1;
a = 2.5;
MO b=a + 3;

MO c = new int[] { 1, 2, 3 };
a = c + b;

}
}
}
...全文
49 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
上海程序员3 2011-03-26
  • 打赏
  • 举报
回复
何必那么麻烦,直接引用个Microsoft.JScript就可以了。

7,765

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧