C

来自CGTWiki
跳转至:导航搜索

C#常有代码汇总

XML保存与读取

反射与序列化

  • 定义:
    • 反射:是指程序可以访问、检测和修改它本身状态或行为的一种能力,程序集包含模块,而模块包含类型,类型又包含成员。反射则提供了封装程序集、模块和类型的对象,可以通过反射动态地创建类型的实例,将类型绑定到现有的对象,或从现有对象中获取类型,然后可以调用类型的方法或访问其字段和属性。
    • 序列化: 序列化是将对象转换为容易传输的格式的过程,比如转化为二进制、xml、json从而在网络中传输。与序列化相反的是反序列化,它将流转换为对象,也就是将在序列化过程中所产生的二进制串、XML、Json等转换成数据结构或对象的过程。将序列化和反序列化两个过程接合起来,可以轻松地存储、传输数据。
      • 序列化源码
public static string SerializeToString (object obj)
{
	XmlSerializer serializer = new XmlSerializer (obj.GetType ());
 
	using (StringWriter writer = new StringWriter()) {
		serializer.Serialize (writer, obj);
 
		return writer.ToString ();
	}
}
 
public static T SerializeFromString<T> (string xml)
{
	XmlSerializer serializer = new XmlSerializer (typeof(T));
  
	using (StringReader reader = new StringReader(xml)) {
		return (T)serializer.Deserialize (reader);
	}
}
public struct Vector3
{
	public float x;
	public float y;
	public float z;
}
public class TestClass
{
	public int testInt = 1234;
	public float testFloat = 0.2341f;
	public bool[] boolArray = {true, false, false, true};
	public Vector3 position;
	public TestClass ()
	{
		this.position.x = 100;
		this.position.y = 100;
		this.position.z = 0;
	}
}

运行结果:

<?xml version="1.0" encoding="utf-8"?>
<TestClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <testInt>1234</testInt>
  <testFloat>0.2341</testFloat>
  <boolArray>
    <boolean>true</boolean>
    <boolean>false</boolean>
    <boolean>false</boolean>
    <boolean>true</boolean>
  </boolArray>
  <position>
    <x>100</x>
    <y>100</y>
    <z>0</z>
  </position>
</TestClass>

C#映射网络驱动器

[StructLayout(LayoutKind.Sequential)]
        public class NetResource
        {
            public int dwScope;
            public int dwType;
            public int dwDisplayType;
            public int dwUsage;
            public string LocalName;
            public string RemoteName;
            public string Comment;
            public string provider;
        }
        [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern int WNetGetConnection(
            [MarshalAs(UnmanagedType.LPTStr)] string localName,
            [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName,
            ref int length);

        [DllImport("mpr.dll", CharSet = CharSet.Ansi)]
        private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flag);
        [DllImport("mpr.dll", CharSet = CharSet.Ansi)]
        private static extern int WNetCancelConnection2(string lpname, int flag, bool force);

        /// <summary>
        /// 映射网络驱动器
        /// </summary>
        /// <param name="localName">本地盘符 如U:</param>
        /// <param name="remotePath">远程路经 如\\\\172.18.118.106\\f</param>
        /// <param name="userName">远程服务器用户名</param>
        /// <param name="password">远程服务器密码</param>
        /// <returns>true映射成功,false映射失败</returns>
        public static bool WNetReflectDrive(string localName, string remotePath, string userName, string password)
        {
            NetResource netResource = new NetResource();
            netResource.dwScope = 2;
            netResource.dwType = 0x1;
            netResource.dwDisplayType = 3;
            netResource.dwUsage = 1;
            netResource.LocalName = localName;
            netResource.RemoteName = remotePath;
            netResource.provider = null;
            int ret = WNetAddConnection2(netResource, password, userName, 0);
            if (ret == 0)
                return true;
            return false;
        }

隐藏盘符

http://www.codesc.net/source/12241.shtml

system.action的用法