1

I created a generic function that retreives a JSON and parses it:

    public IList<Object> RetrieveView(string result)
    {
        JavaScriptSerializer ser = new JavaScriptSerializer();

        IList<Object> values = ser.Deserialize< IList<Object> >(result);

        return values;
    }

The problem that i have alot of Classes that uses this. for example when i try to use this:

IList<Receipt> receipts = (IList<Receipt>)RetrieveView(result);

I get InvalidCastException Unable to cast object of type 'System.Collections.Generic.List[System.Object]' to type 'System.Collections.Generic.IList[Receipt]'.

Anyway to keep my function Generic and be able to cast it to all of my classes/models?


Javascript function for painting connected walls in labyrinth?

I have a problem for which made simplified code for express it. In few words I need to build code in Javascript for painting connected SVG lines. The simple example:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >

<line x1="50" y1="50" x2="200" y2="50" stroke="steelblue" stroke-width="20" onclick="fillWall(evt)" />
<line x1="100" y1="100" x2="400" y2="100" stroke="steelblue" stroke-width="20" onclick="fillWall(evt)" />
<line x1="300" y1="300" x2="200" y2="300" stroke="steelblue" stroke-width="20" onclick="fillWall(evt)" />
<line x1="100" y1="50" x2="100" y2="400" stroke="steelblue" stroke-width="20" onclick="fillWall(evt)" />
<line x1="300" y1="100" x2="300" y2="300" stroke="steelblue" stroke-width="20" onclick="fillWall(evt)" />
<line x1="200" y1="300" x2="200" y2="200" stroke="steelblue" stroke-width="20" onclick="fillWall(evt)" />

<script type="text/javascript"> 
<![CDATA[
function fillWall(evt) {
  var tgt=evt.target;
  tgt.setAttributeNS(null, "stroke", "firebrick");
}
]]>
</script>
</svg>

This is labyrinth of few walls when you click on some it changes color, so I need to do it with one click to paint all connected, no matter on which wall click is applied. In full sized scale there are almost thousand of these walls, and some are connected, some aren't. I tried to learn recursive functions, but easily exceeded stack size. Please help, I'll appreciate that a huge.

4

2 に答える 2

6

Make your function generic:

    public IList<T> RetrieveView<T>(string result)
    {
        JavaScriptSerializer ser = new JavaScriptSerializer();

        IList<T> values = ser.Deserialize< IList<T> >(result);

        return values;
    }
于 2012-06-16T13:43:43.427 に答える
4

オブジェクトが Receipt として逆シリアル化されている場合は、列挙可能な拡張機能を使用できます。

IList<Receipt> receipts = RetrieveView(result).Cast<Receipt>().ToList();
于 2012-06-16T13:43:49.620 に答える