This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
メソッドには「Ancestors()」や「DescendantNodes()」といった LINQ to XML でお馴染みのメソッドがあります。ソースコード内のメソッド定義にアクセスする場合、これらのメソッドを用いて次のようにアクセスすることができます。
var tree = CSharpSyntaxTree.ParseText(sourceCode);
// メソッド定義を全て取得
var methodDeclarations = tree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>();
using System.IO;
using System.Xml.Serialization;
namespace XmlSerializerSample
{
public class SerializeTarget
{
private SerializeTarget () {}
public SerializeTarget(int value1, int value2)
{
Prop1 = value1;
Prop2 = value2;
}
public int Prop1 { get; set; }
public int Prop2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
SerializeTarget target = new SerializeTarget(100, 200);
string serialized;
XmlSerializer xs = new XmlSerializer(typeof(SerializeTarget));
using (StringWriter sw = new StringWriter())
{
xs.Serialize(sw, target);
serialized = sw.ToString();
}
SerializeTarget deserialized;
using (StringReader sr = new StringReader(serialized))
{
deserialized = (SerializeTarget)xs.Deserialize(sr);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// usingは以下の2つを追加
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
namespace RoslynSample
{
class Program
{
// パース対象のコード
private static string m_sourceCode =
@"using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RoslynSample
{
class Program
{
///
/// エントリポイントです。
///
/// コマンドライン引数 static void Main(string[] args)
{
System.Console.WriteLine(""Hello Roslyn!"");
}
}
}";
static void Main(string[] args)
{
// ParseText()にパース対象のコードを渡せば、コードの構文木を作成できる。
// コードをファイルで指定する場合はParseFile()を使用する。
SyntaxTree tree = CSharpSyntaxTree.ParseText(m_sourceCode);
}
}
}