180 lines
4.3 KiB
C#
180 lines
4.3 KiB
C#
using DrawGraph;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using System.Runtime.Serialization;
|
||
using System.Runtime.Serialization.Formatters.Binary;
|
||
using System.Windows.Forms;
|
||
using System.Xml.Serialization;
|
||
|
||
|
||
namespace DrawGraph
|
||
{
|
||
public class BoardUtil
|
||
{
|
||
public const string tagFlag = "_";
|
||
|
||
public static T Clone<T>(T RealObject)
|
||
{
|
||
MemoryStream memoryStream = new MemoryStream();
|
||
T result;
|
||
try
|
||
{
|
||
BinaryFormatter binaryFormatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
|
||
binaryFormatter.Serialize(memoryStream, RealObject);
|
||
memoryStream.Position = 0L;
|
||
result = (T)((object)binaryFormatter.Deserialize(memoryStream));
|
||
}
|
||
catch (Exception )
|
||
{
|
||
result = default(T);
|
||
memoryStream.Close();
|
||
}
|
||
finally
|
||
{
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static PackObjBase PackObjFactory(string instanceName, ZedGraphControl parameter, PackObjManager parameter1)
|
||
{
|
||
PackObjBase result = null;
|
||
try
|
||
{
|
||
Assembly executingAssembly = Assembly.GetExecutingAssembly();
|
||
object[] args = new object[]
|
||
{
|
||
parameter,
|
||
parameter1
|
||
};
|
||
result = (PackObjBase)executingAssembly.CreateInstance("DrawGraph." + instanceName, true, BindingFlags.Default, null, args, null, null);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
string text = ex.Message.ToString();
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static Control CreatFormControlFactory(string instanceName)
|
||
{
|
||
Control control = null;
|
||
Control result;
|
||
try
|
||
{
|
||
string assemblyQualifiedName = typeof(TextBox).AssemblyQualifiedName;
|
||
string newAss = BoardUtil.GetNewAss(assemblyQualifiedName, instanceName);
|
||
Type type;
|
||
try
|
||
{
|
||
type = Type.GetType(newAss);
|
||
bool flag = type == null;
|
||
if (flag)
|
||
{
|
||
MessageBox.Show("请输入控件名,注意大小写,如:Label,Button");
|
||
result = control;
|
||
return result;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
result = control;
|
||
return result;
|
||
}
|
||
control = (Control)Activator.CreateInstance(type);
|
||
}
|
||
catch
|
||
{
|
||
}
|
||
result = control;
|
||
return result;
|
||
}
|
||
|
||
private static string GetNewAss(string str, string sName)
|
||
{
|
||
int num = str.IndexOf(',');
|
||
string text = str.Substring(0, num);
|
||
string str2 = str.Substring(num);
|
||
string str3 = text.Replace("TextBox", sName);
|
||
return str3 + str2;
|
||
}
|
||
|
||
public static void ZedControlClear(ZedGraphControl zedGraphControl1)
|
||
{
|
||
bool flag = zedGraphControl1 != null;
|
||
if (flag)
|
||
{
|
||
zedGraphControl1.GraphPane.Chart.Rect = new RectangleF(0f, 0f, 0f, 0f);
|
||
zedGraphControl1.GraphPane.Title.Text = "";
|
||
}
|
||
}
|
||
|
||
public static bool SaveFile<T>(T t, string filePath, ref string msg)
|
||
{
|
||
Stream stream = null;
|
||
bool result = true;
|
||
try
|
||
{
|
||
stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
|
||
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T), new Type[]
|
||
{
|
||
typeof(T),
|
||
typeof(T)
|
||
});
|
||
xmlSerializer.Serialize(stream, t);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
msg = ex.InnerException.Message;
|
||
result = false;
|
||
}
|
||
finally
|
||
{
|
||
bool flag = stream != null;
|
||
if (flag)
|
||
{
|
||
stream.Close();
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static void MainToSub<TMain, TSub>(TMain Main, TSub sub) where TSub : TMain
|
||
{
|
||
string name = "";
|
||
Type type = Main.GetType();
|
||
Type type2 = sub.GetType();
|
||
PropertyInfo[] properties = type.GetProperties();
|
||
PropertyInfo[] properties2 = type2.GetProperties();
|
||
for (int i = 0; i < properties.Length; i++)
|
||
{
|
||
name = properties[i].Name;
|
||
bool canRead = properties[i].CanRead;
|
||
if (canRead)
|
||
{
|
||
object value = properties[i].GetValue(Main, BindingFlags.GetProperty, null, null, null);
|
||
PropertyInfo propertyInfo = properties2.Where((a, b) => a.Name == name).FirstOrDefault<PropertyInfo>();
|
||
bool flag = propertyInfo != null && propertyInfo.CanWrite;
|
||
if (flag)
|
||
{
|
||
propertyInfo.SetValue(sub, value, BindingFlags.SetProperty, null, null, null);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public static Point GetCurrScrollVal(Panel zedControl)
|
||
{
|
||
return new Point
|
||
{
|
||
X = zedControl.HorizontalScroll.Value,
|
||
Y = zedControl.VerticalScroll.Value
|
||
};
|
||
}
|
||
}
|
||
}
|