本文整理汇总了C#中TypeDeclaration.Clone方法的典型用法代码示例。如果您正苦于以下问题:C# TypeDeclaration.Clone方法的具体用法?C# TypeDeclaration.Clone怎么用?C# TypeDeclaration.Clone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TypeDeclaration
的用法示例。
在下文中一共展示了TypeDeclaration.Clone方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Set
static TypeCode Set (DocumentRef doc, TypeDeclaration rtypedecl, CSharpAstResolver resolver)
{
var rawCode = GetCommentlessCode (rtypedecl);
var typedecl = (TypeDeclaration)rtypedecl.Clone ();
var ns = rtypedecl.Parent as NamespaceDeclaration;
var nsName = ns == null ? "" : ns.FullName;
var name = rtypedecl.Name;
var usings =
resolver.RootNode.Descendants.
OfType<UsingDeclaration> ().
Select (x => x.ToString ().Trim ()).
ToList ();
//
// Find dependencies
//
var deps = new List<String> ();
foreach (var d in rtypedecl.Descendants.OfType<SimpleType> ()) {
deps.Add (d.Identifier);
}
//
// Find watches and instrument
//
var watches = new List<WatchVariable> ();
foreach (var d in typedecl.Descendants.OfType<VariableInitializer> ()) {
var endLoc = d.EndLocation;
var p = d.Parent;
if (p == null || p.Parent == null)
continue;
var nc = p.GetNextSibling (x => x is Comment && x.StartLocation.Line == endLoc.Line);
if (nc == null || !nc.ToString ().StartsWith ("//="))
continue;
var id = Guid.NewGuid ().ToString ();
var instrument = GetWatchInstrument (id, new IdentifierExpression (d.Name));
p.Parent.InsertChildBefore (nc, instrument, BlockStatement.StatementRole);
watches.Add (new WatchVariable {
Id = id,
Expression = d.Name,
ExplicitExpression = "",
FilePath = doc.FullPath,
FileLine = nc.StartLocation.Line,
FileColumn = nc.StartLocation.Column,
});
}
foreach (var d in typedecl.Descendants.OfType<AssignmentExpression> ()) {
var endLoc = d.EndLocation;
var p = d.Parent;
if (p == null || p.Parent == null)
continue;
var nc = p.GetNextSibling (x => x is Comment && x.StartLocation.Line == endLoc.Line);
if (nc == null || !nc.ToString ().StartsWith ("//="))
continue;
var id = Guid.NewGuid ().ToString ();
var instrument = GetWatchInstrument (id, (Expression)d.Left.Clone ());
p.Parent.InsertChildBefore (nc, instrument, BlockStatement.StatementRole);
watches.Add (new WatchVariable {
Id = id,
Expression = d.Left.ToString (),
ExplicitExpression = "",
FilePath = doc.FullPath,
FileLine = nc.StartLocation.Line,
FileColumn = nc.StartLocation.Column,
});
}
foreach (var d in typedecl.Descendants.OfType<Comment> ().Where (x => x.CommentType == CommentType.SingleLine)) {
var m = WatchVariable.CommentContentRe.Match (d.Content);
if (!m.Success || string.IsNullOrWhiteSpace (m.Groups [1].Value))
continue;
var p = d.Parent as BlockStatement;
if (p == null)
continue;
var exprText = m.Groups [1].Value.Trim ();
var parser = new CSharpParser();
var syntaxTree = parser.Parse("class C { void F() { var __r = " + exprText + "; } }");
if (syntaxTree.Errors.Count > 0)
continue;
var t = syntaxTree.Members.OfType<TypeDeclaration> ().First ();
var expr = t.Descendants.OfType<VariableInitializer> ().First ().Initializer;
var id = Guid.NewGuid ().ToString ();
var instrument = GetWatchInstrument (id, expr.Clone ());
p.InsertChildBefore (d, instrument, BlockStatement.StatementRole);
watches.Add (new WatchVariable {
Id = id,
Expression = exprText,
ExplicitExpression = m.Groups[1].Value,
FilePath = doc.FullPath,
FileLine = d.StartLocation.Line,
FileColumn = d.StartLocation.Column,
});
}
//.........这里部分代码省略.........
示例2: GetCommentlessCode
static string GetCommentlessCode (TypeDeclaration rtypedecl)
{
var t = (TypeDeclaration)rtypedecl.Clone ();
var cs = t.Descendants.OfType<Comment> ().ToList ();
foreach (var c in cs) {
var m = WatchVariable.CommentContentRe.Match (c.Content);
if (m.Success) {
c.Content = m.Groups[1].Value + m.Groups[2].Value;
} else {
c.Remove ();
}
}
return t.ToString ();
}