|
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | 5 | using System; |
| 6 | +using System.Reflection; |
| 7 | +using System.Collections; |
| 8 | +using System.Collections.Generic; |
6 | 9 | using System.Runtime.CompilerServices; |
7 | 10 | using Xunit; |
8 | 11 |
|
@@ -110,6 +113,72 @@ internal class HasCctorReceiver |
110 | 113 | { |
111 | 114 | public static string S; |
112 | 115 | } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] |
| 119 | + public static void PrepareMethod() |
| 120 | + { |
| 121 | + foreach (MethodInfo m in typeof(RuntimeHelpersTests).GetMethods()) |
| 122 | + RuntimeHelpers.PrepareMethod(m.MethodHandle); |
| 123 | + |
| 124 | + Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(default(RuntimeMethodHandle))); |
| 125 | + Assert.ThrowsAny<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(IList).GetMethod("Add").MethodHandle)); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public static void PrepareGenericMethod() |
| 130 | + { |
| 131 | + Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(default(RuntimeMethodHandle), null)); |
| 132 | + |
| 133 | + // |
| 134 | + // Type instantiations |
| 135 | + // |
| 136 | + |
| 137 | + // Generic definition with instantiation is valid |
| 138 | + RuntimeHelpers.PrepareMethod(typeof(List<>).GetMethod("Add").MethodHandle, |
| 139 | + new RuntimeTypeHandle[] { typeof(TestStruct).TypeHandle }); |
| 140 | + |
| 141 | + // Instantiated method without instantiation is valid |
| 142 | + RuntimeHelpers.PrepareMethod(typeof(List<int>).GetMethod("Add").MethodHandle, |
| 143 | + null); |
| 144 | + |
| 145 | + // Generic definition without instantiation is invalid |
| 146 | + Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(List<>).GetMethod("Add").MethodHandle, |
| 147 | + null)); |
| 148 | + |
| 149 | + // Wrong instantiation |
| 150 | + Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(List<>).GetMethod("Add").MethodHandle, |
| 151 | + new RuntimeTypeHandle[] { typeof(TestStruct).TypeHandle, typeof(TestStruct).TypeHandle })); |
| 152 | + |
| 153 | + // |
| 154 | + // Method instantiations |
| 155 | + // |
| 156 | + |
| 157 | + // Generic definition with instantiation is valid |
| 158 | + RuntimeHelpers.PrepareMethod(typeof(Array).GetMethod("Resize").MethodHandle, |
| 159 | + new RuntimeTypeHandle[] { typeof(TestStruct).TypeHandle }); |
| 160 | + |
| 161 | + // Instantiated method without instantiation is valid |
| 162 | + RuntimeHelpers.PrepareMethod(typeof(Array).GetMethod("Resize") |
| 163 | + .MakeGenericMethod(new Type[] { typeof(TestStruct) }).MethodHandle, |
| 164 | + null); |
| 165 | + |
| 166 | + // Generic definition without instantiation is invalid |
| 167 | + Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(Array).GetMethod("Resize").MethodHandle, |
| 168 | + null)); |
| 169 | + |
| 170 | + // Wrong instantiation |
| 171 | + Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(Array).GetMethod("Resize").MethodHandle, |
| 172 | + new RuntimeTypeHandle[] { typeof(TestStruct).TypeHandle, typeof(TestStruct).TypeHandle })); |
| 173 | + } |
| 174 | + |
| 175 | + [Fact] |
| 176 | + public static void PrepareDelegate() |
| 177 | + { |
| 178 | + RuntimeHelpers.PrepareDelegate((Action)(() => { })); |
| 179 | + RuntimeHelpers.PrepareDelegate((Func<int>)(() => 1) + (Func<int>)(() => 2)); |
| 180 | + RuntimeHelpers.PrepareDelegate(null); |
| 181 | + } |
113 | 182 | } |
114 | 183 |
|
115 | 184 | public struct Age |
|
0 commit comments