序列化,JSON,反射??? var buff = new MemoryStream(64); var arr = new List<test>(2); arr.Add(new test(null, 2)); arr.Add(new test(string.Empty, 3)); var json = new DataContractJsonSerializer(arr.GetType()); json.WriteObject(buff, arr); Console.WriteLine(Encoding.UTF8.GetString(buff.GetBuffer())); chaircat<[email protected]> 16:48:04 .net 3.5, System.Runtime.Serialization.Json
始终没有好好地利用反射做些事情...
=====
反射把enum的成员列出来显示在list里面,
同时GetType得到类型,
再然后可以构造sql的where语句...
够形象了吧?
实例代码2[关于反射]
if (!String.IsNullOrEmpty(action)) { string FileName = Request.FilePath; FileName = FileName.Substring(FileName.LastIndexOf('/') + 1, FileName.LastIndexOf('.') - 1); Type thisType = Type.GetType("Cpush.Web." + FileName, true, true); System.Reflection.MethodInfo method = thisType.GetMethod("action_" + action); if (method != null) method.Invoke(thisType.Assembly.CreateInstance(thisType.FullName), null); }
2.给 通俗的说下 委托 委托就是代码地址 或叫函数指针
3.System.Web.HttpCookieCollection reqcookie = Request.Cookies; System.Web.HttpCookieCollection respcookie = new HttpCookieCollection(); for(int i = 0;i<reqcookie.Count;i++) { if (reqcookie[i].Name != "ASP.NET_SessionId") { System.Web.HttpCookie cookie = new HttpCookie(reqcookie[i].Name); cookie.Expires = DateTime.Now;---可改成cookie.Expires = DateTime.MinValue; 客户端时间不一定准。 respcookie.Add(cookie); } } for (int i = 0;i<respcookie.Count;i++) { Response.Cookies.Add(respcookie[i]); }
4.List<(Of <(T>)>) 类是 ArrayList 类的泛型等效类。该类使用大小可按需动态增加的数组实现 IList<(Of <(T>)>) 泛型接口。
5.--sort自定义排序(非默认)sort(Icomparer接口参数) 下面的代码示例演示如何使用 IComparer 接口对 ArrayList 对象进行排序。在此示例中,通过使用 CaseInsensitiveComparer 类将 ArrayList 中内容的顺序反转来实现 IComparer 接口。 using System; using System.Collections; public class SamplesArrayList { public class myReverserClass : IComparer { // Calls CaseInsensitiveComparer.Compare with the parameters reversed. int IComparer.Compare( Object x, Object y ) { return( (new CaseInsensitiveComparer()).Compare( y, x ) ); } } public static void Main() { // Creates and initializes a new ArrayList. ArrayList myAL = new ArrayList(); myAL.Add( "The" ); myAL.Add( "quick" ); myAL.Add( "brown" ); myAL.Add( "fox" ); myAL.Add( "jumps" ); myAL.Add( "over" ); myAL.Add( "the" ); myAL.Add( "lazy" ); myAL.Add( "dog" ); // Displays the values of the ArrayList. Console.WriteLine( "The ArrayList initially contains the following values:" ); PrintIndexAndValues( myAL ); // Sorts the values of the ArrayList using the default comparer. myAL.Sort(); Console.WriteLine( "After sorting with the default comparer:" ); PrintIndexAndValues( myAL ); // Sorts the values of the ArrayList using the reverse case-insensitive comparer. IComparer myComparer = new myReverserClass(); myAL.Sort( myComparer ); Console.WriteLine( "After sorting with the reverse case-insensitive comparer:" ); PrintIndexAndValues( myAL ); } public static void PrintIndexAndValues( IEnumerable myList ) { int i = 0; foreach ( Object obj in myList ) Console.WriteLine( "/t[{0}]:/t{1}", i++, obj ); Console.WriteLine(); } } /* This code produces the following output. The ArrayList initially contains the following values: [0]: The [1]: quick [2]: brown [3]: fox [4]: jumps [5]: over [6]: the [7]: lazy [8]: dog After sorting with the default comparer: [0]: brown [1]: dog [2]: fox [3]: jumps [4]: lazy [5]: over [6]: quick [7]: the [8]: The After sorting with the reverse case-insensitive comparer: [0]: the [1]: The [2]: quick [3]: over [4]: lazy [5]: jumps [6]: fox [7]: dog [8]: brown */