Information Technology Reference
In-Depth Information
if
(xmlSource !=
null
)
result =
new
DynamicXElement
(
xmlSource.Element(
XName
.Get(binder.Name)));
return true
;
}
public override bool
TryGetIndex(
GetIndexBinder
binder,
object
[] indexes,
out object
result)
{
result =
null
;
// This only supports [string, int] indexers
if
(indexes.Length !=
2
)
return false
;
if
(!(indexes[
0
]
is string
))
return false
;
if
(!(indexes[
1
]
is int
))
return false
;
var
allNodes = xmlSource.Elements(indexes[
0
].
ToString());
int
index = (
int
)indexes[
1
];
if
(index < allNodes.Count())
result =
new
DynamicXElement
(allNodes.ElementAt(
index));
else
result =
new
DynamicXElement
(
null
);
return true
;
}
public override string
ToString()
{
if
(xmlSource !=
null
)
return
xmlSource.ToString();
else
return string
.Empty;
}
}
Most of the code uses similar concepts to the code you have seen earlier in
this item. The TryGetIndex method is new. It must implement the dynamic
behavior when client code invokes an indexer to retrieve an XElement.