|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | using System; |
5 | | -using System.Runtime.CompilerServices; |
6 | 5 |
|
7 | | -#if false |
8 | | - |
9 | | -public class MyBox<T> |
10 | | -{ |
11 | | - private T _value; |
12 | | - public T Value { get => _value; private set { _value = value; }} |
13 | | - public MyBox(T inp) { Value = inp; } |
14 | | -} |
15 | | - |
16 | | -public class SimpleBox |
| 6 | +namespace HelloWorld |
17 | 7 | { |
18 | | - private decimal _value; |
19 | | - public decimal Value { get => _value; private set { _value = value;}} |
20 | | - public SimpleBox(decimal inp) { Value = inp;} |
21 | | -} |
22 | | - |
23 | | -public class Program |
24 | | -{ |
25 | | - public static void Main() |
| 8 | + internal class Program |
26 | 9 | { |
27 | | - var sbox = new SimpleBox((decimal)-5); |
28 | | - RunSimple(sbox, (decimal)20); |
29 | | - Console.WriteLine ("20 = {0}", sbox.Value); |
30 | | - var box = new MyBox<string>("xyz"); |
31 | | - RunItAgain<string>(box, "hjk"); |
32 | | - Console.WriteLine ("hjk = {0}", box.Value); |
33 | | - var box2 = new MyBox<decimal>((decimal)-2); |
34 | | - RunItAgain<decimal>(box2, (decimal)10.0); |
35 | | - Console.WriteLine (box2.Value); |
36 | | - RunIt<string>(box, "abc"); |
37 | | - Console.WriteLine ("abc = {0}", box.Value); |
38 | | - } |
39 | | - |
40 | | - [MethodImpl(MethodImplOptions.NoInlining)] |
41 | | - public static void RunIt<H> (MyBox<H> dest, H input) |
42 | | - { |
43 | | - ref H boxWriter = ref AccessBox(dest); |
44 | | - boxWriter = input; |
45 | | - } |
46 | | - |
47 | | - [MethodImpl(MethodImplOptions.NoInlining)] |
48 | | - public static void RunItAgain<S> (MyBox<S> dest, S input) |
49 | | - { |
50 | | - ref S boxWriter = ref AccessHelper<S>.AccessBox2(dest); |
51 | | - boxWriter = input; |
52 | | - } |
53 | | - |
54 | | - [UnsafeAccessor(UnsafeAccessorKind.Field, Name="_value")] |
55 | | - private static extern ref W AccessBox<W>(MyBox<W> x); |
56 | | - |
57 | | - [MethodImpl(MethodImplOptions.NoInlining)] |
58 | | - private static void RunSimple(SimpleBox s, decimal input) |
59 | | - { |
60 | | - ref decimal boxWriter = ref SimpleHelper.AccessSimpleBox(s); |
61 | | - boxWriter = input; |
62 | | - } |
63 | | -} |
64 | | - |
65 | | -public class AccessHelper<Q> |
66 | | -{ |
67 | | -#if false |
68 | | - [MethodImpl(MethodImplOptions.NoInlining)] |
69 | | - public static /*extern*/ ref Q AccessBox2(MyBox<Q> q) => throw new NotImplementedException("exn"); |
70 | | -#else |
71 | | - [UnsafeAccessor(UnsafeAccessorKind.Field, Name="_value")] |
72 | | - public static extern ref Q AccessBox2(MyBox<Q> q); |
73 | | -#endif |
74 | | -} |
75 | | - |
76 | | -public class SimpleHelper |
77 | | -{ |
78 | | - [UnsafeAccessor(UnsafeAccessorKind.Field, Name="_value")] |
79 | | - public static extern ref decimal AccessSimpleBox(SimpleBox b); |
80 | | -} |
81 | | - |
82 | | -#else |
83 | | - |
84 | | -public class MyService<T> |
85 | | -{ |
86 | | - public MyService() {} |
87 | | - |
88 | | - private static string Explain(T arg) => arg.ToString() + " : " + typeof(T).ToString(); //$"{arg} : {typeof(T)}"; |
89 | | -} |
90 | | - |
91 | | -public class Program |
92 | | -{ |
93 | | - public static void Main() |
| 10 | + private static void Main(string[] args) |
94 | 11 | { |
95 | | - new Program().Runner<string>("abcd"); |
| 12 | + bool isMono = typeof(object).Assembly.GetType("Mono.RuntimeStructs") != null; |
| 13 | + Console.WriteLine($"Hello World {(isMono ? "from Mono!" : "from CoreCLR!")}"); |
| 14 | + Console.WriteLine(typeof(object).Assembly.FullName); |
| 15 | + Console.WriteLine(System.Reflection.Assembly.GetEntryAssembly ()); |
| 16 | + Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription); |
96 | 17 | } |
97 | | - |
98 | | - [MethodImpl(MethodImplOptions.NoInlining)] |
99 | | - public void Runner<Q>(Q x) |
100 | | - { |
101 | | - Console.WriteLine (AccessHelper<Q>.CallExplain(default, x)); |
102 | | - } |
103 | | - |
104 | | -} |
105 | | - |
106 | | -public static class AccessHelper<W> |
107 | | -{ |
108 | | - [UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name="Explain")] |
109 | | - public static extern string CallExplain(MyService<W> target, W arg); |
110 | | - |
| 18 | + } |
111 | 19 | } |
112 | | - |
113 | | -#endif |
0 commit comments