JavaScript: How To Get Anonymous Types From WCF

來源:互聯網
上載者:User

Welcome to My WordPress Blog

In C# 3.5, We can create anonymous types to improve our efficiency, usually we do it like this:

var coords = new { X=1.5f, Y=2.0f };
Console.WriteLine( coords.X );
coords.X = 1.0f;

Using anonymous types, we do not need to define classes one by one. And even, we can combine several anonymous type values to one new value:

var coords = new { X=1.5f, Y=2.0f };
var point = new { Coord = coords, Color = "Red" };

It’s easy to use to keep temporary values or create new value in linq expressions, but, as anonymous type is internal, we cannot return anonymous type value from methods, the following code is invalid:

static object ReturnAnonymousTypeValue()
{
return new { X = 1.5f, Y = 2.0f };
}
static void ReadAnonymousTypeValueFromAntherMethod()
{
var val = ReturnAnonymousTypeValue();
Console.WriteLine(val.X); //Invalid
}

(In C# 4.0, we can use “dynamic” to reach the goal.)

In method ReturnAnonymousTypeValue, as the returnd value has no type name, the returnd type is object. also we can not cast the object value to “some type” in the same reason. We lose the data… No solution to solve it. There are too many limits of anonymous types.

Now, I have a requirement of getting data from WCF but I do not want to create class(Too tedious and inflexible), and I want to do it like this:

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(...)]
public class Task
{
[OperationContract]
[WebInvoke(...)]
public object Test(bool s)
{
return new { Enabled = s, Name = "Test" };
}
}

Then get the data using ajax:

$.ajax({
url: 'Task.svc/Test',
data: '{"s":true}',
type: 'post',
dataType: 'json',
contentType: 'text/json',
success: function(data) { alert( $.toJSON( data.d ) ); }
});

It obviously cannot work, when invoke the method, exception throws, because DataContractSerializer cannot Serialize anonymous types(also class with no attribute “DataContract”).

We can only code like this: 

[ServiceContract(Namespace = "")][AspNetCompatibilityRequirements(...)]
public class Task{
[OperationContract]
[WebInvoke(...)]
public TestType Test(bool s)
{
return new TestType { Enabled = s, Name = "Test" };
}
}

[DataContract]
public class TestType{
[DataMember]
bool Enabled{ get; set; }
[DataMember]
string Name{ get; set; }
}

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);
}
}

Modify the WCF:

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(...)]
public class Task
{
[OperationContract]
[WebInvoke(...)]
public string Test(bool s)
{
return new { Enabled = true, Name = "Test" }.ToJson();
}
}

The method Test returns one string with json format and you can serialize anything and return it!

In browser, change the success callback:

success: function(data) { var jd = $.evalJSON( data.d ); alert(jd.Enabled); }

As data.d is the return string with json format, Just get json value from data.d using $.evalJSON.
Look! Isn’t it elegent? We can return any data easily with no complex class definition.

Download:

Reference:

  • JavaScriptSerializer
  • Jquery Json PlugIn
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.