Apr 12, 2013

Returning anonymous from WCF

Can C# return anonymous types from methods? NO! It cannot in any case, not to mention WCF need serialize the value. So, we should look for other ways. In general, if we want to serialize one valuable, the type of the val should take the attributes [Serializable] or [DataContract] or other relateds, or implement the interface ISerializable. Anonymous types cannot reach the goal. But there is one special class in .Net Framework “JavaScriptSerializer” in System.Web.Script.Serialization,System.Web.Extensions.dll, which can serialize any types to json format string! ******************************** public static class JsonSerialization { public static string ToJson(this object data) { return new JavaScriptSerializer().Serialize(data); } } -------------------------------------------- [ServiceContract(Namespace = "")] [AspNetCompatibilityRequirements(...)] public class Task { [OperationContract] [WebInvoke(...)] public string Test(bool s) { return new { Enabled = true, Name = "Test" }.ToJson(); } }

Mar 19, 2013

Generics

http://stackoverflow.com/questions/692540/examples-of-usage-of-generics-in-net-c-vb-net http://www.dotnetperls.com/generic