c# 封装成类似动态类型,类似matlab的尝试
本来想发到技术区,分数不够了,发这里算了,
自己粗粗实现了一些,
大家帮着看看有没有更好的方法,
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;
}
}
}
自己粗粗实现了一些,
大家帮着看看有没有更好的方法,
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;
}
}
}
...全文
相关推荐
c#调用matlab步骤.doc
封装在这个部分,我们将使用MATLAB的封装工具箱将MATLAB函数封装成DLL文件。首先,我们需要在命令行窗口输入“deploytool”命令,调出封装工具箱。然后,选择相应的选项,并点击确定按钮。
C#调用Matlab编译的DLL
通过Matlab的编译器,如MATLAB Compiler,可以将.m文件转换成可由其他编程语言(如C#)调用的DLL动态链接库。**2.
CTP接口 C/C#/Java封装,20140325更新
**MATLAB事件处理**:与C#类似,MATLAB中也可以使用事件驱动模型来处理CTP接口的回调。
将Matlab图形窗口嵌入C#窗体,C#嵌入Matlab图形
总结:通过上述步骤,我们可以将Matlab的图形窗口嵌入到C#窗体中,使得C#应用能够利用Matlab的图形处理能力。
C# 调用MATLAB程序绘图
当我们需要在C#应用中利用MATLAB的绘图功能时,可以借助MATLAB的COM接口或者.NET工厂来实现。以下将详细讲解如何在C#中调用MATLAB程序进行绘图。


非技术区
