I'm trying to use a Dictionary in a C# script block embedded in a XSLT1.0 doc. When I'm trying to add a reference to either System.Collections.Generic or System.Core, I'm getting a 'metadata file not found for assembly...' When adding ref to 'mscorlib', it seems like an older version is being referenced (?), since I'm getting an error when trying to use Dictionary.Keys.Contains() ('does not contain a Contains method). Needless to say, this code was tested successfully outside the xslt. How can I add a proper ref to a FW 3.5/4 ? Tnx!
1 Answer
Dictionary.Keys.Contains is defined in the System.Linq namespace, and Linq is defined in the System.Core assembly, so you'll need the right msxsl:assembly and msxsl:using elements for those. The following works for me in the VS2013 XSLT debugger:
<msxsl:script implements-prefix="my" language="csharp">
<msxsl:assembly name="System.Core" />
<msxsl:using namespace="System.Linq" />
<msxsl:using namespace="System.Collections.Generic" />
<![CDATA[
public bool myFunc()
{
var d = new Dictionary<string, string>();
d.Add("Hello", "Goodbye");
return d.Keys.Contains("Hello");
}
]]>
</msxsl:script>
This still leaves the question of why you are using Dictionary.Keys.Contains, when you could use Dictionary.ContainsKey, which has been in .NET since 2.0 and doesn't need any extra assembly or namespace references.
1 Comment
Mani Klein
Thanks.Using ContainsKey indeed solved the 'no method' issue, however I'm still unable to reference System.Linq, Collections.Generic or Core. Any ideas? VS2010 btw