本文整理汇总了C#中Microsoft.CodeAnalysis.CodeGen.CompilationTestData.GetMethodData方法的典型用法代码示例。如果您正苦于以下问题:C# CompilationTestData.GetMethodData方法的具体用法?C# CompilationTestData.GetMethodData怎么用?C# CompilationTestData.GetMethodData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CodeGen.CompilationTestData
的用法示例。
在下文中一共展示了CompilationTestData.GetMethodData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DebugOnly
public void DebugOnly()
{
var source =
@"class C
{
static System.IDisposable F()
{
return null;
}
static void M()
{
lock (F()) { }
using (F()) { }
}
}";
var debug = CreateCompilationWithMscorlib(source, options: TestOptions.DebugDll);
var release = CreateCompilationWithMscorlib(source, options: TestOptions.ReleaseDll);
CompilationTestData testData;
ImmutableArray<string> names;
testData = new CompilationTestData();
debug.EmitToArray(testData: testData);
names = GetLocalNames(testData.GetMethodData("C.M"));
AssertEx.Equal(new string[] { "CS$2$0000", "CS$520$0001", "CS$3$0002" }, names);
testData = new CompilationTestData();
release.EmitToArray(testData: testData);
names = GetLocalNames(testData.GetMethodData("C.M"));
AssertEx.Equal(new string[] { null, null }, names);
}
示例2: AddressOfLocal
public void AddressOfLocal()
{
var source =
@"class C
{
void M()
{
string s = ""hello"";
}
}";
var comp = CreateCompilationWithMscorlib(source, options: TestOptions.DebugDll);
WithRuntimeInstance(comp, runtime =>
{
var context = CreateMethodContext(runtime, "C.M");
var testData = new CompilationTestData();
string error;
context.CompileExpression("&s", out error, testData);
Assert.Null(error);
var methodData = testData.GetMethodData("<>x.<>m0");
AssertIsIntPtrPointer(methodData.Method.ReturnType);
methodData.VerifyIL(@"
{
// Code size 4 (0x4)
.maxstack 1
.locals init (string V_0) //s
IL_0000: ldloca.s V_0
IL_0002: conv.u
IL_0003: ret
}
");
});
}
示例3: AddressOfParameter
public void AddressOfParameter()
{
var source =
@"class C
{
void M(string s)
{
}
}";
var comp = CreateCompilationWithMscorlib(source, options: TestOptions.DebugDll);
var runtime = CreateRuntimeInstance(comp);
var context = CreateMethodContext(
runtime,
methodName: "C.M");
var testData = new CompilationTestData();
string error;
context.CompileExpression("&s", out error, testData);
Assert.Null(error);
var methodData = testData.GetMethodData("<>x.<>m0");
AssertIsIntPtrPointer(methodData.Method.ReturnType);
methodData.VerifyIL(@"
{
// Code size 4 (0x4)
.maxstack 1
IL_0000: ldarga.s V_1
IL_0002: conv.u
IL_0003: ret
}
");
}
示例4: Win8RuntimeAssemblies
public void Win8RuntimeAssemblies()
{
var source =
@"class C
{
static void M(Windows.Storage.StorageFolder f, Windows.Foundation.Collections.PropertySet p)
{
}
}";
var compilation0 = CreateCompilationWithMscorlib(
source,
options: TestOptions.DebugDll,
assemblyName: ExpressionCompilerUtilities.GenerateUniqueName(),
references: WinRtRefs);
var runtimeAssemblies = ExpressionCompilerTestHelpers.GetRuntimeWinMds("Windows.Storage", "Windows.Foundation.Collections");
Assert.True(runtimeAssemblies.Length >= 2);
byte[] exeBytes;
byte[] pdbBytes;
ImmutableArray<MetadataReference> references;
compilation0.EmitAndGetReferences(out exeBytes, out pdbBytes, out references);
var runtime = CreateRuntimeInstance(
ExpressionCompilerUtilities.GenerateUniqueName(),
ImmutableArray.Create(MscorlibRef).Concat(runtimeAssemblies), // no reference to Windows.winmd
exeBytes,
new SymReader(pdbBytes));
var context = CreateMethodContext(runtime, "C.M");
ResultProperties resultProperties;
string error;
var testData = new CompilationTestData();
context.CompileExpression("(p == null) ? f : null", out resultProperties, out error, testData);
Assert.Null(error);
testData.GetMethodData("<>x.<>m0").VerifyIL(
@"{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.1
IL_0001: brfalse.s IL_0005
IL_0003: ldnull
IL_0004: ret
IL_0005: ldarg.0
IL_0006: ret
}");
}
示例5: ExplicitEmbeddedType
public void ExplicitEmbeddedType()
{
var source =
@"using System.Runtime.InteropServices;
[TypeIdentifier]
[Guid(""863D5BC0-46A1-49AD-97AA-A5F0D441A9D8"")]
public interface I
{
object F();
}
class C
{
void M()
{
var o = (I)null;
}
static void Main()
{
(new C()).M();
}
}";
var compilation0 = CSharpTestBase.CreateCompilationWithMscorlib(
source,
options: TestOptions.DebugExe,
assemblyName: ExpressionCompilerUtilities.GenerateUniqueName());
var runtime = CreateRuntimeInstance(compilation0);
var context = CreateMethodContext(runtime, "C.M");
ResultProperties resultProperties;
string error;
var testData = new CompilationTestData();
var result = context.CompileExpression("this", out resultProperties, out error, testData);
Assert.Null(error);
testData.GetMethodData("<>x.<>m0").VerifyIL(
@"{
// Code size 2 (0x2)
.maxstack 1
.locals init (I V_0) //o
IL_0000: ldarg.0
IL_0001: ret
}");
}
示例6: Literal
public void Literal()
{
var source =
@"class C
{
static void M()
{
(int, int) o;
}
}";
var comp = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef, SystemRuntimeFacadeRef }, options: TestOptions.DebugDll);
WithRuntimeInstance(comp, new[] { ValueTupleRef, SystemRuntimeFacadeRef, MscorlibRef }, runtime =>
{
var context = CreateMethodContext(runtime, "C.M");
var testData = new CompilationTestData();
string error;
var result = context.CompileExpression("(A: 1, B: 2)", out error, testData);
Assert.Null(error);
ReadOnlyCollection<byte> customTypeInfo;
var customTypeInfoId = result.GetCustomTypeInfo(out customTypeInfo);
ReadOnlyCollection<byte> dynamicFlags;
ReadOnlyCollection<string> tupleElementNames;
CustomTypeInfo.Decode(customTypeInfoId, customTypeInfo, out dynamicFlags, out tupleElementNames);
Assert.Equal(new[] { "A", "B" }, tupleElementNames);
var methodData = testData.GetMethodData("<>x.<>m0");
var method = methodData.Method;
Assert.True(method.ReturnType.IsTupleType);
Assert.NotNull(GetTupleElementNamesAttributeIfAny(method));
methodData.VerifyIL(
@"{
// Code size 8 (0x8)
.maxstack 2
.locals init ((int, int) V_0) //o
IL_0000: ldc.i4.1
IL_0001: ldc.i4.2
IL_0002: newobj ""System.ValueTuple<int, int>..ctor(int, int)""
IL_0007: ret
}");
});
}
示例7: ConstantEnumAndTypeParameter
public void ConstantEnumAndTypeParameter()
{
var source =
@"class C<T>
{
enum E { A }
internal static void M<U>() where U : T
{
const C<T>.E t = E.A;
const C<U>.E u = 0;
}
}
class P
{
static void Main()
{
C<object>.M<string>();
}
}";
var compilation0 = CreateCompilationWithMscorlib(source, options: TestOptions.DebugExe);
byte[] exeBytes;
byte[] pdbBytes;
ImmutableArray<MetadataReference> references;
compilation0.EmitAndGetReferences(out exeBytes, out pdbBytes, out references);
var constantSignatures = ImmutableDictionary.CreateRange(
new Dictionary<string, byte[]>()
{
{ "t", new byte[] { 0x15, 0x11, 0x10, 0x01, 0x13, 0x00 } },
{ "u", new byte[] { 0x15, 0x11, 0x10, 0x01, 0x1e, 0x00 } }
});
var runtime = CreateRuntimeInstance(ExpressionCompilerUtilities.GenerateUniqueName(), references, exeBytes, new SymReader(pdbBytes, constantSignatures));
var context = CreateMethodContext(
runtime,
methodName: "C.M");
var testData = new CompilationTestData();
var locals = ArrayBuilder<LocalAndMethod>.GetInstance();
string typeName;
context.CompileGetLocals(locals, argumentsOnly: false, typeName: out typeName, testData: testData);
Assert.Equal(locals.Count, 3);
VerifyLocal(testData, "<>x<T>", locals[0], "<>m0<U>", "t", DkmClrCompilationResultFlags.ReadOnlyResult, expectedILOpt:
@"{
// Code size 2 (0x2)
.maxstack 1
IL_0000: ldc.i4.0
IL_0001: ret
}",
expectedGeneric: true);
VerifyLocal(testData, "<>x<T>", locals[1], "<>m1<U>", "u", DkmClrCompilationResultFlags.ReadOnlyResult, expectedILOpt:
@"{
// Code size 2 (0x2)
.maxstack 1
IL_0000: ldc.i4.0
IL_0001: ret
}",
expectedGeneric: true);
VerifyLocal(testData, "<>x<T>", locals[2], "<>m2<U>", "<>TypeVariables", DkmClrCompilationResultFlags.ReadOnlyResult, expectedILOpt:
@"{
// Code size 6 (0x6)
.maxstack 1
IL_0000: newobj ""<>c__TypeVariables<T, U>..ctor()""
IL_0005: ret
}",
expectedGeneric: true);
testData.GetMethodData("<>c__TypeVariables<T, U>..ctor").VerifyIL(
@"{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call ""object..ctor()""
IL_0006: ret
}");
locals.Free();
}
示例8: ExtensionIterator
public void ExtensionIterator()
{
var source = @"
static class C
{
static System.Collections.IEnumerable F(this int x)
{
yield return x;
}
}
";
var expectedIL = @"
{
// Code size 7 (0x7)
.maxstack 1
.locals init (int V_0,
bool V_1)
IL_0000: ldarg.0
IL_0001: ldfld ""int C.<F>d__0.x""
IL_0006: ret
}";
var compilation0 = CreateCompilationWithMscorlibAndSystemCore(source, options: TestOptions.DebugDll);
var runtime = CreateRuntimeInstance(compilation0);
var context = CreateMethodContext(
runtime,
methodName: "C.<F>d__0.MoveNext");
var testData = new CompilationTestData();
var locals = ArrayBuilder<LocalAndMethod>.GetInstance();
string typeName;
var assembly = context.CompileGetLocals(locals, argumentsOnly: false, typeName: out typeName, testData: testData);
Assert.NotNull(assembly);
Assert.NotEqual(assembly.Count, 0);
Assert.Equal(locals.Count, 1);
VerifyLocal(testData, typeName, locals[0], "<>m0", "x", expectedILOpt: expectedIL);
Assert.Equal(SpecialType.System_Int32, testData.GetMethodData(typeName + ".<>m0").Method.ReturnType.SpecialType);
locals.Free();
testData = new CompilationTestData();
string error;
context.CompileExpression("x", out error, testData);
Assert.Null(error);
var methodData = testData.GetMethodData("<>x.<>m0");
methodData.VerifyIL(expectedIL);
Assert.Equal(SpecialType.System_Int32, methodData.Method.ReturnType.SpecialType);
}
示例9: NoLocalSignature
public void NoLocalSignature()
{
var source =
@"class C
{
void M(int[] a)
{
string b;
a[1]++;
lock (new C())
{
#line 999
int c = 3;
b = a[c].ToString();
}
}
}";
var compilation0 = CreateCompilationWithMscorlib(source, options: TestOptions.DebugDll);
byte[] exeBytes;
byte[] pdbBytes;
ImmutableArray<MetadataReference> references;
compilation0.EmitAndGetReferences(out exeBytes, out pdbBytes, out references);
var runtime = CreateRuntimeInstance(
ExpressionCompilerUtilities.GenerateUniqueName(),
references,
exeBytes,
new SymReader(pdbBytes),
includeLocalSignatures: false);
var context = CreateMethodContext(
runtime,
methodName: "C.M",
atLineNumber: 999);
var testData = new CompilationTestData();
var locals = ArrayBuilder<LocalAndMethod>.GetInstance();
string typeName;
var assembly = context.CompileGetLocals(locals, argumentsOnly: false, typeName: out typeName, testData: testData);
Assert.Equal(locals.Count, 2);
VerifyLocal(testData, typeName, locals[0], "<>m0", "this", expectedILOpt:
@"{
// Code size 2 (0x2)
.maxstack 1
IL_0000: ldarg.0
IL_0001: ret
}");
VerifyLocal(testData, typeName, locals[1], "<>m1", "a", expectedILOpt:
@"{
// Code size 2 (0x2)
.maxstack 1
IL_0000: ldarg.1
IL_0001: ret
}");
locals.Free();
ResultProperties resultProperties;
string error;
testData = new CompilationTestData();
context.CompileExpression("b", out resultProperties, out error, testData);
Assert.Equal(error, "error CS0103: The name 'b' does not exist in the current context");
testData = new CompilationTestData();
context.CompileExpression("a[1]", out resultProperties, out error, testData);
string actualIL = testData.GetMethodData("<>x.<>m0").GetMethodIL();
AssertEx.AssertEqualToleratingWhitespaceDifferences(actualIL,
@"{
// Code size 4 (0x4)
.maxstack 2
IL_0000: ldarg.1
IL_0001: ldc.i4.1
IL_0002: ldelem.i4
IL_0003: ret
}");
}
示例10: TypeOutsideModule
public void TypeOutsideModule()
{
var sourceA =
@"using System;
public class A<T>
{
public static void M(Action f)
{
object o;
try
{
f();
}
catch (Exception)
{
}
}
}";
var sourceB =
@"using System;
class E : Exception
{
internal object F;
}
class B
{
static void Main()
{
A<int>.M(() => { throw new E(); });
}
}";
var assemblyNameA = "0A93FF0B-31A2-47C8-B24D-16A2D77AB5C5";
var compilationA = CreateCompilationWithMscorlib(sourceA, options: TestOptions.DebugDll, assemblyName: assemblyNameA);
byte[] exeA;
byte[] pdbA;
ImmutableArray<MetadataReference> referencesA;
compilationA.EmitAndGetReferences(out exeA, out pdbA, out referencesA);
var metadataA = AssemblyMetadata.CreateFromImage(exeA);
var referenceA = metadataA.GetReference();
var assemblyNameB = "9BAC6622-86EB-4EC5-94A1-9A1E6D0C24B9";
var compilationB = CreateCompilationWithMscorlib(sourceB, options: TestOptions.DebugExe, references: new[] { referenceA }, assemblyName: assemblyNameB);
byte[] exeB;
byte[] pdbB;
ImmutableArray<MetadataReference> referencesB;
compilationB.EmitAndGetReferences(out exeB, out pdbB, out referencesB);
var metadataB = AssemblyMetadata.CreateFromImage(exeB);
var referenceB = metadataB.GetReference();
var modulesBuilder = ArrayBuilder<ModuleInstance>.GetInstance();
modulesBuilder.Add(MscorlibRef.ToModuleInstance(fullImage: null, symReader: null));
modulesBuilder.Add(referenceA.ToModuleInstance(fullImage: exeA, symReader: new SymReader(pdbA)));
modulesBuilder.Add(referenceB.ToModuleInstance(fullImage: exeB, symReader: new SymReader(pdbB)));
modulesBuilder.Add(ExpressionCompilerTestHelpers.IntrinsicAssemblyReference.ToModuleInstance(fullImage: null, symReader: null));
using (var runtime = new RuntimeInstance(modulesBuilder.ToImmutableAndFree()))
{
var context = CreateMethodContext(
runtime,
"A.M");
var aliases = ImmutableArray.Create(
ExceptionAlias("E, 9BAC6622-86EB-4EC5-94A1-9A1E6D0C24B9, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
ObjectIdAlias(1, "A`1[[B, 9BAC6622-86EB-4EC5-94A1-9A1E6D0C24B9, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], 0A93FF0B-31A2-47C8-B24D-16A2D77AB5C5, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"));
string error;
var testData = new CompilationTestData();
context.CompileExpression(
"$exception",
DkmEvaluationFlags.TreatAsExpression,
aliases,
out error,
testData);
Assert.Null(error);
testData.GetMethodData("<>x<T>.<>m0").VerifyIL(
@"{
// Code size 11 (0xb)
.maxstack 1
.locals init (object V_0) //o
IL_0000: call ""System.Exception Microsoft.VisualStudio.Debugger.Clr.IntrinsicMethods.GetException()""
IL_0005: castclass ""E""
IL_000a: ret
}");
ResultProperties resultProperties;
ImmutableArray<AssemblyIdentity> missingAssemblyIdentities;
testData = new CompilationTestData();
context.CompileAssignment(
"o",
"$1",
aliases,
DiagnosticFormatter.Instance,
out resultProperties,
out error,
out missingAssemblyIdentities,
EnsureEnglishUICulture.PreferredOrNull,
testData);
Assert.Empty(missingAssemblyIdentities);
Assert.Null(error);
testData.GetMethodData("<>x<T>.<>m0").VerifyIL(
@"{
// Code size 17 (0x11)
.maxstack 1
//.........这里部分代码省略.........
示例11: DifferentAssemblyVersion
public void DifferentAssemblyVersion()
{
var sourceA =
@"public class A<T>
{
}";
var sourceB =
@"class B<T>
{
}
class C
{
static void M()
{
var o = new A<object>();
}
}";
var assemblyNameA = "397300B0-A";
var publicKeyA = ImmutableArray.CreateRange(new byte[] { 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, 0x06, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 0x31, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0xED, 0xD3, 0x22, 0xCB, 0x6B, 0xF8, 0xD4, 0xA2, 0xFC, 0xCC, 0x87, 0x37, 0x04, 0x06, 0x04, 0xCE, 0xE7, 0xB2, 0xA6, 0xF8, 0x4A, 0xEE, 0xF3, 0x19, 0xDF, 0x5B, 0x95, 0xE3, 0x7A, 0x6A, 0x28, 0x24, 0xA4, 0x0A, 0x83, 0x83, 0xBD, 0xBA, 0xF2, 0xF2, 0x52, 0x20, 0xE9, 0xAA, 0x3B, 0xD1, 0xDD, 0xE4, 0x9A, 0x9A, 0x9C, 0xC0, 0x30, 0x8F, 0x01, 0x40, 0x06, 0xE0, 0x2B, 0x95, 0x62, 0x89, 0x2A, 0x34, 0x75, 0x22, 0x68, 0x64, 0x6E, 0x7C, 0x2E, 0x83, 0x50, 0x5A, 0xCE, 0x7B, 0x0B, 0xE8, 0xF8, 0x71, 0xE6, 0xF7, 0x73, 0x8E, 0xEB, 0x84, 0xD2, 0x73, 0x5D, 0x9D, 0xBE, 0x5E, 0xF5, 0x90, 0xF9, 0xAB, 0x0A, 0x10, 0x7E, 0x23, 0x48, 0xF4, 0xAD, 0x70, 0x2E, 0xF7, 0xD4, 0x51, 0xD5, 0x8B, 0x3A, 0xF7, 0xCA, 0x90, 0x4C, 0xDC, 0x80, 0x19, 0x26, 0x65, 0xC9, 0x37, 0xBD, 0x52, 0x81, 0xF1, 0x8B, 0xCD });
var compilationA1 = CreateCompilation(
new AssemblyIdentity(assemblyNameA, new Version(1, 1, 1, 1), cultureName: "", publicKeyOrToken: publicKeyA, hasPublicKey: true),
new[] { sourceA },
references: new[] { MscorlibRef_v20 },
options: TestOptions.DebugDll.WithDelaySign(true));
var referenceA1 = compilationA1.EmitToImageReference();
var assemblyNameB = "397300B0-B";
var compilationB1 = CreateCompilation(
new AssemblyIdentity(assemblyNameB, new Version(1, 2, 2, 2)),
new[] { sourceB },
references: new[] { MscorlibRef_v20, referenceA1 },
options: TestOptions.DebugDll);
// Use mscorlib v4.0.0.0 and A v2.1.2.1 at runtime.
byte[] exeBytes;
byte[] pdbBytes;
ImmutableArray<MetadataReference> references;
compilationB1.EmitAndGetReferences(out exeBytes, out pdbBytes, out references);
var compilationA2 = CreateCompilation(
new AssemblyIdentity(assemblyNameA, new Version(2, 1, 2, 1), cultureName: "", publicKeyOrToken: publicKeyA, hasPublicKey: true),
new[] { sourceA },
references: new[] { MscorlibRef_v20 },
options: TestOptions.DebugDll.WithDelaySign(true));
var referenceA2 = compilationA2.EmitToImageReference();
var runtime = CreateRuntimeInstance(
assemblyNameB,
ImmutableArray.Create(MscorlibRef, referenceA2).AddIntrinsicAssembly(),
exeBytes,
new SymReader(pdbBytes));
//// typeof(Exception), typeof(A<B<object>>), typeof(B<A<object>[]>)
var context = CreateMethodContext(
runtime,
"C.M");
var aliases = ImmutableArray.Create(
ExceptionAlias("System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"),
ObjectIdAlias(1, "A`1[[B`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], 397300B0-B, Version=1.2.2.2, Culture=neutral, PublicKeyToken=null]], 397300B0-A, Version=2.1.2.1, Culture=neutral, PublicKeyToken=1f8a32457d187bf3"),
ObjectIdAlias(2, "B`1[[A`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]][], 397300B0-A, Version=2.1.2.1, Culture=neutral, PublicKeyToken=1f8a32457d187bf3]], 397300B0-B, Version=1.2.2.2, Culture=neutral, PublicKeyToken=null"));
string error;
var testData = new CompilationTestData();
context.CompileExpression(
"(object)$exception ?? (object)$1 ?? $2",
DkmEvaluationFlags.TreatAsExpression,
aliases,
out error,
testData);
testData.GetMethodData("<>x.<>m0").VerifyIL(
@"{
// Code size 44 (0x2c)
.maxstack 2
.locals init (A<object> V_0) //o
IL_0000: call ""System.Exception Microsoft.VisualStudio.Debugger.Clr.IntrinsicMethods.GetException()""
IL_0005: dup
IL_0006: brtrue.s IL_002b
IL_0008: pop
IL_0009: ldstr ""$1""
IL_000e: call ""object Microsoft.VisualStudio.Debugger.Clr.IntrinsicMethods.GetObjectByAlias(string)""
IL_0013: castclass ""A<B<object>>""
IL_0018: dup
IL_0019: brtrue.s IL_002b
IL_001b: pop
IL_001c: ldstr ""$2""
IL_0021: call ""object Microsoft.VisualStudio.Debugger.Clr.IntrinsicMethods.GetObjectByAlias(string)""
IL_0026: castclass ""B<A<object>[]>""
IL_002b: ret
}");
}
示例12: PassByRef
public void PassByRef()
{
var source =
@"class C
{
static T F<T>(ref T t)
{
t = default(T);
return t;
}
}";
var compilation0 = CreateCompilationWithMscorlib(
source,
options: TestOptions.DebugDll,
assemblyName: ExpressionCompilerUtilities.GenerateUniqueName());
var runtime = CreateRuntimeInstance(compilation0);
var context = CreateMethodContext(
runtime,
"C.F");
var aliases = ImmutableArray.Create(
ExceptionAlias(),
ReturnValueAlias(),
ObjectIdAlias(1),
VariableAlias("x", typeof(int)));
string error;
// $exception
context.CompileExpression(
"$exception = null",
DkmEvaluationFlags.TreatAsExpression,
aliases,
out error);
Assert.Equal(error, "error CS0131: The left-hand side of an assignment must be a variable, property or indexer");
context.CompileExpression(
"F(ref $exception)",
DkmEvaluationFlags.TreatAsExpression,
aliases,
out error);
Assert.Equal(error, "error CS1510: A ref or out argument must be an assignable variable");
// Object at address
context.CompileExpression(
"@0x123 = null",
DkmEvaluationFlags.TreatAsExpression,
aliases,
out error);
Assert.Equal(error, "error CS0131: The left-hand side of an assignment must be a variable, property or indexer");
context.CompileExpression(
"F(ref @0x123)",
DkmEvaluationFlags.TreatAsExpression,
aliases,
out error);
Assert.Equal(error, "error CS1510: A ref or out argument must be an assignable variable");
// $ReturnValue
context.CompileExpression(
"$ReturnValue = null",
DkmEvaluationFlags.TreatAsExpression,
aliases,
out error);
Assert.Equal(error, "error CS0131: The left-hand side of an assignment must be a variable, property or indexer");
context.CompileExpression(
"F(ref $ReturnValue)",
DkmEvaluationFlags.TreatAsExpression,
aliases,
out error);
Assert.Equal(error, "error CS1510: A ref or out argument must be an assignable variable");
// Object id
context.CompileExpression(
"$1 = null",
DkmEvaluationFlags.TreatAsExpression,
aliases,
out error);
Assert.Equal(error, "error CS0131: The left-hand side of an assignment must be a variable, property or indexer");
context.CompileExpression(
"F(ref $1)",
DkmEvaluationFlags.TreatAsExpression,
aliases,
out error);
Assert.Equal(error, "error CS1510: A ref or out argument must be an assignable variable");
// Declared variable
var testData = new CompilationTestData();
context.CompileExpression(
"x = 1",
DkmEvaluationFlags.TreatAsExpression,
aliases,
out error,
testData);
Assert.Null(error);
testData.GetMethodData("<>x.<>m0<T>").VerifyIL(
@"{
// Code size 16 (0x10)
.maxstack 3
.locals init (T V_0,
int V_1)
IL_0000: ldstr ""x""
IL_0005: call ""int Microsoft.VisualStudio.Debugger.Clr.IntrinsicMethods.GetVariableAddress<int>(string)""
IL_000a: ldc.i4.1
//.........这里部分代码省略.........
示例13: CompileTimeAndRuntimeAssemblies
private void CompileTimeAndRuntimeAssemblies(
ImmutableArray<MetadataReference> compileReferences,
ImmutableArray<MetadataReference> runtimeReferences,
string storageAssemblyName)
{
var source =
@"class C
{
static void M(LibraryA.A a, LibraryB.B b, Windows.Data.Text.TextSegment t, Windows.Storage.StorageFolder f)
{
}
}";
var compilation0 = CreateCompilationWithMscorlib(source, compileReferences, TestOptions.DebugDll);
WithRuntimeInstance(compilation0, runtimeReferences, runtime =>
{
var context = CreateMethodContext(runtime, "C.M");
string error;
var testData = new CompilationTestData();
context.CompileExpression("(object)a ?? (object)b ?? (object)t ?? f", out error, testData);
Assert.Null(error);
testData.GetMethodData("<>x.<>m0").VerifyIL(
@"{
// Code size 17 (0x11)
.maxstack 2
IL_0000: ldarg.0
IL_0001: dup
IL_0002: brtrue.s IL_0010
IL_0004: pop
IL_0005: ldarg.1
IL_0006: dup
IL_0007: brtrue.s IL_0010
IL_0009: pop
IL_000a: ldarg.2
IL_000b: dup
IL_000c: brtrue.s IL_0010
IL_000e: pop
IL_000f: ldarg.3
IL_0010: ret
}");
testData = new CompilationTestData();
var result = context.CompileExpression("default(Windows.Storage.StorageFolder)", out error, testData);
Assert.Null(error);
var methodData = testData.GetMethodData("<>x.<>m0");
methodData.VerifyIL(
@"{
// Code size 2 (0x2)
.maxstack 1
IL_0000: ldnull
IL_0001: ret
}");
// Check return type is from runtime assembly.
var assemblyReference = AssemblyMetadata.CreateFromImage(result.Assembly).GetReference();
var compilation = CSharpCompilation.Create(
assemblyName: ExpressionCompilerUtilities.GenerateUniqueName(),
references: runtimeReferences.Concat(ImmutableArray.Create<MetadataReference>(assemblyReference)));
var assembly = ImmutableArray.CreateRange(result.Assembly);
using (var metadata = ModuleMetadata.CreateFromImage(ImmutableArray.CreateRange(assembly)))
{
var reader = metadata.MetadataReader;
var typeDef = reader.GetTypeDef("<>x");
var methodHandle = reader.GetMethodDefHandle(typeDef, "<>m0");
var module = (PEModuleSymbol)compilation.GetMember("<>x").ContainingModule;
var metadataDecoder = new MetadataDecoder(module);
SignatureHeader signatureHeader;
BadImageFormatException metadataException;
var parameters = metadataDecoder.GetSignatureForMethod(methodHandle, out signatureHeader, out metadataException);
Assert.Equal(parameters.Length, 5);
var actualReturnType = parameters[0].Type;
Assert.Equal(actualReturnType.TypeKind, TypeKind.Class); // not error
var expectedReturnType = compilation.GetMember("Windows.Storage.StorageFolder");
Assert.Equal(expectedReturnType, actualReturnType);
Assert.Equal(storageAssemblyName, actualReturnType.ContainingAssembly.Name);
}
});
}
示例14: BaseMembersInLambda
public void BaseMembersInLambda()
{
var source = @"
class Base
{
protected int x;
}
class Derived : Base
{
new protected object x;
void F()
{
System.Action a = () => this.x.ToString();
a();
}
}";
var compilation0 = CreateCompilationWithMscorlib(
source,
options: TestOptions.DebugDll,
assemblyName: ExpressionCompilerUtilities.GenerateUniqueName());
var runtime = CreateRuntimeInstance(compilation0);
var context = CreateMethodContext(runtime, "Derived.<F>b__1_0");
string error;
var testData = new CompilationTestData();
context.CompileExpression("this.x", out error, testData);
testData.GetMethodData("<>x.<>m0").VerifyIL(@"
{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: ldfld ""object Derived.x""
IL_0006: ret
}");
}
示例15: BaseMembersInAsync
public void BaseMembersInAsync()
{
var source = @"
using System;
using System.Threading.Tasks;
class Base
{
protected int x;
}
class Derived : Base
{
new protected object x;
async Task M()
{
await Console.Out.WriteLineAsync(this.ToString());
}
}";
var compilation0 = CreateCompilationWithMscorlib45(
source,
options: TestOptions.DebugDll,
assemblyName: ExpressionCompilerUtilities.GenerateUniqueName());
var runtime = CreateRuntimeInstance(compilation0);
var context = CreateMethodContext(runtime, "Derived.<M>d__1.MoveNext");
string error;
var testData = new CompilationTestData();
context.CompileExpression("base.x", out error, testData);
testData.GetMethodData("<>x.<>m0").VerifyIL(@"
{
// Code size 12 (0xc)
.maxstack 1
.locals init (int V_0,
System.Runtime.CompilerServices.TaskAwaiter V_1,
Derived.<M>d__1 V_2,
System.Exception V_3)
IL_0000: ldarg.0
IL_0001: ldfld ""Derived Derived.<M>d__1.<>4__this""
IL_0006: ldfld ""int Base.x""
IL_000b: ret
}");
}