本文整理匯總了C#中System.Xml.Linq.XElement.Annotation方法的典型用法代碼示例。如果您正苦於以下問題:C# XElement.Annotation方法的具體用法?C# XElement.Annotation怎麽用?C# XElement.Annotation使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Xml.Linq.XElement
的用法示例。
在下文中一共展示了XElement.Annotation方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: ResolveNamespace
public void ResolveNamespace(XElement elem, XamlContext ctx) {
if (Namespace != null)
return;
// Since XmlnsProperty records are inside the element,
// the namespace is resolved after processing the element body.
string xmlNs = null;
if (elem.Annotation<XmlnsScope>() != null)
xmlNs = elem.Annotation<XmlnsScope>().LookupXmlns(Assembly, TypeNamespace);
if (xmlNs == null)
xmlNs = ctx.XmlNs.LookupXmlns(Assembly, TypeNamespace);
if (xmlNs == null) {
var nsSeg = TypeNamespace.Split('.');
var nsName = nsSeg[nsSeg.Length - 1].ToLowerInvariant();
var prefix = nsName;
int count = 0;
while (elem.GetNamespaceOfPrefix(prefix) != null) {
count++;
prefix = nsName + count;
}
xmlNs = string.Format("clr-namespace:{0};assembly={1}", TypeNamespace, Assembly);
elem.Add(new XAttribute(XNamespace.Xmlns + XmlConvert.EncodeLocalName(prefix),
ctx.GetXmlNamespace(xmlNs)));
}
Namespace = xmlNs;
}
示例2: GetInstance
public static ASupportElement GetInstance(XElement definition)
{
Type eType = ElementType (definition);
if (eType != null) {
if (definition.Annotation (eType) != null)
return definition.Annotation (eType) as ASupportElement;
else {
var instance = Activator.CreateInstance (eType, new object[] { definition });
definition.AddAnnotation (instance);
return instance as ASupportElement;
}
} else
return null;
}
示例3: RewriteClass
void RewriteClass(XamlContext ctx, XElement elem) {
var type = elem.Annotation<XamlType>();
if (type == null || type.ResolvedType == null)
return;
var typeDef = type.ResolvedType.ResolveTypeDef();
var comparer = new AssemblyNameComparer(AssemblyNameComparerFlags.All);
if (typeDef == null || !comparer.Equals(typeDef.DefinitionAssembly, ctx.Module.Assembly))
return;
var newType = typeDef.BaseType;
var xamlType = new XamlType(newType.DefinitionAssembly, newType.ReflectionNamespace, newType.Name);
xamlType.ResolveNamespace(elem, ctx);
elem.Name = xamlType.ToXName(ctx);
var attrName = ctx.GetXamlNsName("Class", elem);
var attrs = elem.Attributes().ToList();
if (typeDef.IsNotPublic) {
var classModifierName = ctx.GetXamlNsName("ClassModifier", elem);
attrs.Insert(0, new XAttribute(classModifierName, ctx.BamlDecompilerOptions.InternalClassModifier));
}
attrs.Insert(0, new XAttribute(attrName, type.ResolvedType.ReflectionFullName));
elem.ReplaceAttributes(attrs);
}
示例4: RewriteElement
bool RewriteElement(XamlContext ctx, XElement parent, XElement elem) {
var type = parent.Annotation<XamlType>();
var property = elem.Annotation<XamlProperty>();
if ((property == null || type == null) && elem.Name != key)
return false;
if (elem.Elements().Count() != 1 || elem.Attributes().Any(t => t.Name.Namespace != XNamespace.Xmlns))
return false;
var value = elem.Elements().Single();
if (!CanInlineExt(ctx, value))
return false;
var ext = InlineExtension(ctx, value);
if (ext == null)
return false;
ctx.CancellationToken.ThrowIfCancellationRequested();
var extValue = ext.ToString(ctx, parent);
var attrName = elem.Name;
if (attrName != key)
attrName = property.ToXName(ctx, parent, property.IsAttachedTo(type));
var attr = new XAttribute(attrName, extValue);
parent.Add(attr);
elem.Remove();
return true;
}
示例5: ResolveNamespace
public void ResolveNamespace(XElement elem, XamlContext ctx) {
if (Namespace != null)
return;
// Since XmlnsProperty records are inside the element,
// the namespace is resolved after processing the element body.
string xmlNs = null;
if (elem.Annotation<XmlnsScope>() != null)
xmlNs = elem.Annotation<XmlnsScope>().LookupXmlns(Assembly, TypeNamespace);
if (xmlNs == null)
xmlNs = ctx.XmlNs.LookupXmlns(Assembly, TypeNamespace);
// Sometimes there's no reference to System.Xaml even if x:Type is used
if (xmlNs == null)
xmlNs = ctx.TryGetXmlNamespace(Assembly, TypeNamespace);
if (xmlNs == null) {
if (AssemblyNameComparer.CompareAll.Equals(Assembly, ctx.Module.Assembly))
xmlNs = $"clr-namespace:{TypeNamespace}";
else
xmlNs = $"clr-namespace:{TypeNamespace};assembly={Assembly.Name}";
var nsSeg = TypeNamespace.Split('.');
var prefix = nsSeg[nsSeg.Length - 1].ToLowerInvariant();
if (string.IsNullOrEmpty(prefix)) {
if (string.IsNullOrEmpty(TypeNamespace))
prefix = "global";
else
prefix = "empty";
}
int count = 0;
var truePrefix = prefix;
XNamespace prefixNs, ns = ctx.GetXmlNamespace(xmlNs);
while ((prefixNs = elem.GetNamespaceOfPrefix(truePrefix)) != null && prefixNs != ns) {
count++;
truePrefix = prefix + count;
}
if (prefixNs == null) {
elem.Add(new XAttribute(XNamespace.Xmlns + XmlConvert.EncodeLocalName(truePrefix), ns));
if (string.IsNullOrEmpty(TypeNamespace))
elem.AddBeforeSelf(new XComment(string.Format(dnSpy_BamlDecompiler_Resources.Msg_GlobalNamespace, truePrefix)));
}
}
Namespace = ctx.GetXmlNamespace(xmlNs);
}
示例6: AddUvmlAnnotations
/// <summary>
/// Adds UVML annotations to the specified UVML element tree.
/// </summary>
/// <param name="dataSourceTypeName">The name of the data source wrapper associated with this tree.</param>
/// <param name="root">The root of the UVML element tree to annotate.</param>
public static void AddUvmlAnnotations(String dataSourceTypeName, XElement root)
{
Contract.RequireNotEmpty(dataSourceTypeName, nameof(dataSourceTypeName));
Contract.Require(root, nameof(root));
var templates = 0;
if (root.Annotation<UvmlMetadataAnnotation>() != null)
return;
root.AddAnnotation(new UvmlMetadataAnnotation());
AddUvmlAnnotations(dataSourceTypeName, root, ref templates);
}
示例7: RemoveAllButSpecificFields
private static void RemoveAllButSpecificFields(XElement root, string[] fieldTypesToRetain)
{
var cachedAnnotationInformation = root.Annotation<Dictionary<int, List<XElement>>>();
List<XElement> runsToKeep = new List<XElement>();
foreach (var item in cachedAnnotationInformation)
{
var runsForField = root
.Descendants()
.Where(d =>
{
Stack<FieldRetriever.FieldElementTypeInfo> stack = d.Annotation<Stack<FieldRetriever.FieldElementTypeInfo>>();
if (stack == null)
return false;
if (stack.Any(stackItem => stackItem.Id == item.Key))
return true;
return false;
})
.Select(d => d.AncestorsAndSelf(W.r).FirstOrDefault())
.GroupAdjacent(o => o)
.Select(g => g.First())
.ToList();
foreach (var r in runsForField)
runsToKeep.Add(r);
}
foreach (var paragraph in root.Descendants(W.p).ToList())
{
if (paragraph.Elements(W.r).Any(r => runsToKeep.Contains(r)))
{
paragraph.Elements(W.r)
.Where(r => !runsToKeep.Contains(r) &&
!r.Elements(W.tab).Any())
.Remove();
paragraph.Elements(W.r)
.Where(r => !runsToKeep.Contains(r))
.Elements()
.Where(rc => rc.Name != W.rPr &&
rc.Name != W.tab)
.Remove();
}
else
{
paragraph.Remove();
}
}
root.Descendants(W.tbl).Remove();
}
示例8: RewriteElement
bool RewriteElement(XamlContext ctx, XElement parent, XElement elem) {
var property = elem.Annotation<XamlProperty>();
if (property == null && elem.Name != key)
return false;
if (elem.HasAttributes || elem.HasElements)
return false;
ctx.CancellationToken.ThrowIfCancellationRequested();
var value = elem.Value;
var attrName = elem.Name;
if (attrName != key)
attrName = property.ToXName(ctx, parent, property.IsAttachedTo(parent.Annotation<XamlType>()));
var attr = new XAttribute(attrName, value);
parent.Add(attr);
elem.Remove();
return true;
}
示例9: GetLineInfo
private static LineInfo GetLineInfo(XElement element) {
IXmlLineInfo ie = (IXmlLineInfo) element;
if(ie.HasLineInfo())
return new LineInfo(ie.LineNumber, ie.LinePosition);
else
return element.Annotation<ABB.SrcML.LineInfo>();
}
示例10: AnnotateBlockContentElements
/// Accept deleted paragraphs.
///
/// Group together all paragraphs that contain w:p/w:pPr/w:rPr/w:del elements. Make a
/// second group for the content element immediately following a paragraph that contains
/// a w:del element. The code uses the approach of dealing with paragraph content at
/// 'levels', ignoring paragraph content at other levels. Form a new paragraph that
/// contains the content of the grouped paragraphs with deleted paragraph marks, and the
/// content of the paragraph immediately following a paragraph that contains a deleted
/// paragraph mark. Include in the new paragraph the paragraph properties from the
/// paragraph following. When assembling the new paragraph, use a transform that collapses
/// the paragraph nodes when adding content, thereby preserving custom XML and content
/// controls.
private static void AnnotateBlockContentElements(XElement contentContainer)
{
// For convenience, there is a ParagraphInfo annotation on the contentContainer.
// It contains the same information as the ParagraphInfo annotation on the first
// paragraph.
if (contentContainer.Annotation<BlockContentInfo>() != null)
return;
XElement firstContentElement = contentContainer
.Elements()
.DescendantsAndSelf()
.FirstOrDefault(e => e.Name == W.p || e.Name == W.tbl);
// Add the annotation on the contentContainer.
BlockContentInfo currentContentInfo = new BlockContentInfo()
{
PreviousBlockContentElement = null,
ThisBlockContentElement = firstContentElement,
NextBlockContentElement = null
};
// Add as annotation even though NextParagraph is not set yet.
contentContainer.AddAnnotation(currentContentInfo);
while (true)
{
currentContentInfo.ThisBlockContentElement.AddAnnotation(currentContentInfo);
// Find next sibling content element.
XElement nextContentElement = null;
XElement current = currentContentInfo.ThisBlockContentElement;
while (true)
{
nextContentElement = current
.ElementsAfterSelf()
.DescendantsAndSelf()
.FirstOrDefault(e => e.Name == W.p || e.Name == W.tbl);
if (nextContentElement != null)
{
currentContentInfo.NextBlockContentElement = nextContentElement;
break;
}
current = current.Parent;
// When we've backed up the tree to the contentContainer, we're done.
if (current == contentContainer)
return;
}
currentContentInfo = new BlockContentInfo()
{
PreviousBlockContentElement = currentContentInfo.ThisBlockContentElement,
ThisBlockContentElement = nextContentElement,
NextBlockContentElement = null
};
}
}
示例11: IterateBlockContentElements
private static IEnumerable<BlockContentInfo> IterateBlockContentElements(XElement element)
{
XElement current = element.Elements().FirstOrDefault();
if (current == null)
yield break;
AnnotateBlockContentElements(element);
BlockContentInfo currentBlockContentInfo = element.Annotation<BlockContentInfo>();
while (true)
{
yield return currentBlockContentInfo;
if (currentBlockContentInfo.NextBlockContentElement == null)
yield break;
currentBlockContentInfo = currentBlockContentInfo.NextBlockContentElement.Annotation<BlockContentInfo>();
}
}
示例12: CanInlineExt
bool CanInlineExt(XamlContext ctx, XElement ctxElement) {
var type = ctxElement.Annotation<XamlType>();
if (type != null && type.ResolvedType != null) {
var typeDef = type.ResolvedType.GetBaseType();
bool isExt = false;
while (typeDef != null) {
if (typeDef.FullName == "System.Windows.Markup.MarkupExtension") {
isExt = true;
break;
}
typeDef = typeDef.GetBaseType();
}
if (!isExt)
return false;
}
else if (ctxElement.Annotation<XamlProperty>() == null &&
ctxElement.Name != ctor)
return false;
foreach (var child in ctxElement.Elements()) {
if (!CanInlineExt(ctx, child))
return false;
}
return true;
}
示例13: GetIncludeElementLocation
private Location GetIncludeElementLocation(XElement includeElement, ref string currentXmlFilePath, ref CSharpSyntaxNode originatingSyntax)
{
Location location = includeElement.Annotation<Location>();
if (location != null)
{
return location;
}
// If we are not in an XML file, then we must be in a source file. Since we're traversing the XML tree in the same
// order as the DocumentationCommentWalker, we can access the elements of includeElementNodes in order.
if (currentXmlFilePath == null)
{
Debug.Assert(nextSourceIncludeElementIndex < sourceIncludeElementNodes.Length);
Debug.Assert(originatingSyntax == null);
originatingSyntax = sourceIncludeElementNodes[nextSourceIncludeElementIndex];
location = originatingSyntax.Location;
nextSourceIncludeElementIndex++;
// #line shall not affect the base path:
currentXmlFilePath = location.GetLineSpan().Path;
}
else
{
location = XmlLocation.Create(includeElement, currentXmlFilePath);
}
Debug.Assert(location != null);
includeElement.AddAnnotation(location);
return location;
}
示例14: RetrieveListItem
public static string RetrieveListItem(WordprocessingDocument wordDoc, XElement paragraph, ListItemRetrieverSettings settings)
{
if (wordDoc.MainDocumentPart.NumberingDefinitionsPart == null)
return null;
var listItemInfo = paragraph.Annotation<ListItemInfo>();
if (listItemInfo == null)
InitializeListItemRetriever(wordDoc, settings);
listItemInfo = paragraph.Annotation<ListItemInfo>();
if (!listItemInfo.IsListItem)
return null;
var numberingDefinitionsPart = wordDoc
.MainDocumentPart
.NumberingDefinitionsPart;
if (numberingDefinitionsPart == null)
return null;
StyleDefinitionsPart styleDefinitionsPart = wordDoc
.MainDocumentPart
.StyleDefinitionsPart;
if (styleDefinitionsPart == null)
return null;
var numXDoc = numberingDefinitionsPart.GetXDocument();
var stylesXDoc = styleDefinitionsPart.GetXDocument();
var lvl = listItemInfo.Lvl(GetParagraphLevel(paragraph));
string lvlText = (string)lvl.Elements(W.lvlText).Attributes(W.val).FirstOrDefault();
if (lvlText == null)
return null;
var levelNumbersAnnotation = paragraph.Annotation<LevelNumbers>();
if (levelNumbersAnnotation == null)
throw new OpenXmlPowerToolsException("Internal error");
int[] levelNumbers = levelNumbersAnnotation.LevelNumbersArray;
string languageIdentifier = GetLanguageIdentifier(paragraph, stylesXDoc);
string listItem = FormatListItem(listItemInfo, levelNumbers, GetParagraphLevel(paragraph),
lvlText, stylesXDoc, languageIdentifier, settings);
return listItem;
}
示例15: GetParagraphLevel
public static int GetParagraphLevel(XElement paragraph)
{
var pi = paragraph.Annotation<ParagraphInfo>();
if (pi != null)
return pi.Ilvl;
throw new OpenXmlPowerToolsException("Internal error - should never ask for ilvl without it first being set.");
}