| | | 1 | | using System; |
| | | 2 | | using System.Collections; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Collections.ObjectModel; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Linq.Expressions; |
| | | 8 | | using System.Reflection; |
| | | 9 | | using System.Reflection.Emit; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | using ImpromptuInterface.Optimization; |
| | | 12 | | using Microsoft.CSharp.RuntimeBinder; |
| | | 13 | | |
| | | 14 | | namespace ImpromptuInterface.Build |
| | | 15 | | { |
| | | 16 | | |
| | | 17 | | #if NETFRAMEWORK |
| | | 18 | | public class SaveableActLikeMaker : ActLikeMaker |
| | | 19 | | { |
| | | 20 | | public SaveableActLikeMaker(AssemblyBuilderAccess access, string assemblyName = null) : base(access, assemblyNam |
| | | 21 | | { |
| | | 22 | | |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | public void Save(string dirPath = null) |
| | | 26 | | { |
| | | 27 | | if (AssemblyAccess.HasFlag(AssemblyBuilderAccess.Save)) |
| | | 28 | | { |
| | | 29 | | if (dirPath == null) |
| | | 30 | | { |
| | | 31 | | dirPath = ""; |
| | | 32 | | } |
| | | 33 | | _ab.Save(Path.Combine(dirPath, $"{AssemblyName}.dll")); |
| | | 34 | | } |
| | | 35 | | else |
| | | 36 | | { |
| | | 37 | | throw new InvalidOperationException("AssemblyBuilder not set to be saveable"); |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | #endif |
| | | 43 | | public class ActLikeMaker |
| | | 44 | | { |
| | | 45 | | |
| | | 46 | | |
| | | 47 | | public dynamic ActLike(object originalDynamic, params Type[] otherInterfaces) |
| | | 48 | | { |
| | 25 | 49 | | return new ActLikeCaster(originalDynamic, otherInterfaces) {Maker = this}; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public TInterface Create<TTarget, TInterface>() where TTarget : new() where TInterface : class |
| | | 53 | | { |
| | 0 | 54 | | return this.ActLike<TInterface>(new TTarget()); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public TInterface Create<TTarget, TInterface>(params object[] args) where TInterface : class |
| | | 58 | | { |
| | 14 | 59 | | return this.ActLike(DynamicConstructor.Invoke(typeof(TTarget), args)); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public IEnumerable<TInterface> AllActLike<TInterface>(IEnumerable<object> originalDynamic, params Type[] otherIn |
| | | 63 | | { |
| | 1 | 64 | | return originalDynamic.Select(it => this.ActLike<TInterface>(it, otherInterfaces)); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Private helper method that initializes the proxy. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="proxytype">The proxytype.</param> |
| | | 73 | | /// <param name="original">The original.</param> |
| | | 74 | | /// <param name="interfaces">The interfaces.</param> |
| | | 75 | | /// <param name="propertySpec">The property spec.</param> |
| | | 76 | | /// <returns></returns> |
| | | 77 | | internal static object InitializeProxy(Type proxytype, object original, IEnumerable<Type> interfaces =null, IDic |
| | | 78 | | { |
| | 129 | 79 | | var tProxy = (IActLikeProxyInitialize)Activator.CreateInstance(proxytype); |
| | 129 | 80 | | tProxy.Initialize(original, interfaces, propertySpec, maker); |
| | 129 | 81 | | return tProxy; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public TInterface ActLike<TInterface>(object originalDynamic, params Type[] otherInterfaces) where TInterface : |
| | | 85 | | { |
| | 96 | 86 | | originalDynamic = originalDynamic.GetTargetContext(out var tContext, out var tDummy); |
| | 96 | 87 | | tContext = tContext.FixContext(); |
| | | 88 | | |
| | 96 | 89 | | var tProxy = this.BuildType(tContext, typeof(TInterface), otherInterfaces); |
| | | 90 | | |
| | | 91 | | |
| | | 92 | | |
| | 96 | 93 | | return |
| | 96 | 94 | | (TInterface) |
| | 96 | 95 | | InitializeProxy(tProxy, originalDynamic, new[] {typeof (TInterface)}.Concat(otherInterfaces), maker:this |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | public dynamic ActLikeProperties(object originalDynamic, IDictionary<string, Type> propertySpec) |
| | | 99 | | { |
| | 2 | 100 | | originalDynamic = originalDynamic.GetTargetContext(out var tContext, out var tDummy); |
| | 2 | 101 | | tContext = tContext.FixContext(); |
| | | 102 | | |
| | 2 | 103 | | var tProxy = this.BuildType(tContext, propertySpec); |
| | | 104 | | |
| | | 105 | | |
| | | 106 | | |
| | 2 | 107 | | return |
| | 2 | 108 | | InitializeProxy(tProxy, originalDynamic, propertySpec: propertySpec, maker: this); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | public dynamic DynamicActLike(object originalDynamic, params Type[] otherInterfaces) |
| | | 112 | | { |
| | 31 | 113 | | originalDynamic = originalDynamic.GetTargetContext(out var tContext, out var tDummy); |
| | 31 | 114 | | tContext = tContext.FixContext(); |
| | | 115 | | |
| | 31 | 116 | | var tProxy = this.BuildType(tContext, otherInterfaces.First(), otherInterfaces.Skip(1).ToArray()); |
| | | 117 | | |
| | 31 | 118 | | return InitializeProxy(tProxy, originalDynamic, otherInterfaces, maker: this); |
| | | 119 | | |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | |
| | | 123 | | |
| | | 124 | | |
| | | 125 | | |
| | | 126 | | protected ModuleBuilder _builder; |
| | | 127 | | |
| | | 128 | | protected AssemblyBuilder _ab; |
| | 2 | 129 | | protected readonly IDictionary<TypeHash, Type> _typeHash = new Dictionary<TypeHash, Type>(); |
| | 2 | 130 | | protected readonly object TypeCacheLock = new object(); |
| | | 131 | | |
| | 2 | 132 | | protected readonly IDictionary<TypeHash, Type> _delegateCache = new Dictionary<TypeHash, Type>(); |
| | 2 | 133 | | protected readonly object DelegateCacheLock = new object(); |
| | | 134 | | |
| | 2 | 135 | | protected readonly MethodInfo ActLikeRec = typeof(ActLikeMaker).GetMethod(nameof(RecursiveActLikeForProxy), |
| | 2 | 136 | | new[] {typeof(ActLikeMaker), typeof(object) }); |
| | | 137 | | |
| | | 138 | | public static TInterface RecursiveActLikeForProxy<TInterface>(ActLikeMaker maker, object target) where TInterfac |
| | | 139 | | { |
| | 12 | 140 | | return maker.ActLike<TInterface>(target); |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | |
| | 1 | 144 | | internal ActLikeMaker() |
| | | 145 | | { |
| | 1 | 146 | | AssemblyAccess = AssemblyBuilderAccess.Run; |
| | 1 | 147 | | AssemblyName = $"{nameof(ImpromptuInterface)}DynamicAssembly"; |
| | 1 | 148 | | } |
| | | 149 | | |
| | | 150 | | public string AssemblyName { get; private set; } |
| | | 151 | | |
| | 1 | 152 | | public ActLikeMaker(AssemblyBuilderAccess access, string assemblyName = null) |
| | | 153 | | { |
| | 1 | 154 | | AssemblyAccess = access; |
| | 1 | 155 | | if (assemblyName == null) |
| | | 156 | | { |
| | 1 | 157 | | assemblyName = $"{nameof(ImpromptuInterface)}_{Guid.NewGuid():N}"; |
| | | 158 | | } |
| | | 159 | | |
| | 1 | 160 | | AssemblyName = assemblyName; |
| | 1 | 161 | | } |
| | | 162 | | |
| | | 163 | | ///<summary> |
| | | 164 | | /// Module Builder for building proxies |
| | | 165 | | ///</summary> |
| | | 166 | | private ModuleBuilder Builder |
| | | 167 | | { |
| | | 168 | | get |
| | | 169 | | { |
| | 77 | 170 | | if (_builder == null) |
| | | 171 | | { |
| | | 172 | | |
| | 2 | 173 | | GenerateAssembly(AssemblyName, AssemblyAccess, ref _ab, ref _builder); |
| | | 174 | | } |
| | 77 | 175 | | return _builder; |
| | | 176 | | } |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | |
| | | 180 | | |
| | | 181 | | |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Builds the type for the proxy or returns from cache |
| | | 185 | | /// </summary> |
| | | 186 | | /// <param name="contextType">Type of the context.</param> |
| | | 187 | | /// <param name="mainInterface">The main interface.</param> |
| | | 188 | | /// <param name="otherInterfaces">The other interfaces.</param> |
| | | 189 | | /// <returns></returns> |
| | | 190 | | public Type BuildType(Type contextType, Type mainInterface, params Type[] otherInterfaces) |
| | | 191 | | { |
| | 127 | 192 | | lock (TypeCacheLock) |
| | | 193 | | { |
| | 127 | 194 | | contextType = contextType.FixContext(); |
| | 127 | 195 | | var tNewHash = TypeHash.Create(contextType, new[] { mainInterface }.Concat(otherInterfaces).ToArray()); |
| | | 196 | | Type tType; |
| | 127 | 197 | | if (!_typeHash.TryGetValue(tNewHash, out tType)) |
| | | 198 | | { |
| | 71 | 199 | | tType = BuildTypeHelper(Builder, contextType, new[] { mainInterface }.Concat(otherInterfaces).ToArra |
| | 71 | 200 | | _typeHash[tNewHash] = tType; |
| | | 201 | | } |
| | | 202 | | |
| | 127 | 203 | | return _typeHash[tNewHash]; |
| | | 204 | | } |
| | | 205 | | |
| | 127 | 206 | | } |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Builds the type. |
| | | 210 | | /// </summary> |
| | | 211 | | /// <param name="contextType">Type of the context.</param> |
| | | 212 | | /// <param name="informalInterface">The informal interface.</param> |
| | | 213 | | /// <returns></returns> |
| | | 214 | | public Type BuildType(Type contextType, IDictionary<string, Type> informalInterface) |
| | | 215 | | { |
| | 2 | 216 | | lock (TypeCacheLock) |
| | | 217 | | { |
| | 2 | 218 | | var tNewHash = TypeHash.Create(contextType, informalInterface); |
| | 2 | 219 | | if (!_typeHash.TryGetValue(tNewHash, out var tType)) |
| | | 220 | | { |
| | 2 | 221 | | tType = BuildTypeHelper(Builder, contextType, informalInterface); |
| | | 222 | | |
| | 2 | 223 | | _typeHash[tNewHash] = tType; |
| | | 224 | | } |
| | | 225 | | |
| | 2 | 226 | | return _typeHash[tNewHash]; |
| | | 227 | | } |
| | | 228 | | |
| | 2 | 229 | | } |
| | | 230 | | |
| | | 231 | | /// <summary> |
| | | 232 | | /// Preloads a proxy for ActLike to use. |
| | | 233 | | /// </summary> |
| | | 234 | | /// <param name="proxyType">Type of the proxy.</param> |
| | | 235 | | /// <param name="attribute">The ActLikeProxyAttribute, if not provide it will be looked up.</param> |
| | | 236 | | /// <returns>Returns false if there already is a proxy registered for the same type.</returns> |
| | | 237 | | public bool PreLoadProxy(Type proxyType, ActLikeProxyAttribute attribute = null) |
| | | 238 | | { |
| | 0 | 239 | | var tSuccess = true; |
| | 0 | 240 | | if (attribute == null) |
| | 0 | 241 | | attribute = proxyType.GetCustomAttributes(typeof(ActLikeProxyAttribute), inherit: false).Cast<ActLikePro |
| | | 242 | | |
| | 0 | 243 | | if (attribute == null) |
| | 0 | 244 | | throw new Exception("Proxy Type must have ActLikeProxyAttribute"); |
| | | 245 | | |
| | 0 | 246 | | if (!typeof(IActLikeProxyInitialize).IsAssignableFrom(proxyType)) |
| | 0 | 247 | | throw new Exception("Proxy Type must implement IActLikeProxyInitialize"); |
| | | 248 | | |
| | 0 | 249 | | foreach (var tIType in attribute.Interfaces) |
| | | 250 | | { |
| | 0 | 251 | | if (!tIType.IsAssignableFrom(proxyType)) |
| | | 252 | | { |
| | 0 | 253 | | throw new Exception(String.Format("Proxy Type {0} must implement declared interfaces {1}", proxyType |
| | | 254 | | } |
| | | 255 | | } |
| | | 256 | | |
| | 0 | 257 | | lock (TypeCacheLock) |
| | | 258 | | { |
| | 0 | 259 | | var tNewHash = TypeHash.Create(attribute.Context, attribute.Interfaces); |
| | | 260 | | |
| | 0 | 261 | | if (!_typeHash.ContainsKey(tNewHash)) |
| | | 262 | | { |
| | 0 | 263 | | _typeHash[tNewHash] = proxyType; |
| | | 264 | | } |
| | | 265 | | else |
| | | 266 | | { |
| | 0 | 267 | | tSuccess = false; |
| | | 268 | | } |
| | 0 | 269 | | } |
| | 0 | 270 | | return tSuccess; |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | /// <summary> |
| | | 274 | | /// Preloads proxies that ActLike uses from assembly. |
| | | 275 | | /// </summary> |
| | | 276 | | /// <param name="assembly">The assembly.</param> |
| | | 277 | | /// <returns>Returns false if there already is a proxy registered for the same type.</returns> |
| | | 278 | | public bool PreLoadProxiesFromAssembly(Assembly assembly) |
| | | 279 | | { |
| | 0 | 280 | | var tSuccess = true; |
| | 0 | 281 | | var typesWithMyAttribute = |
| | 0 | 282 | | from tType in assembly.GetTypes() |
| | 0 | 283 | | let tAttributes = tType.GetCustomAttributes(typeof(ActLikeProxyAttribute), inherit: false) |
| | 0 | 284 | | where tAttributes != null && tAttributes.Length == 1 |
| | 0 | 285 | | select new { Type = tType, Impromptu = tAttributes.Cast<ActLikeProxyAttribute>().Single() }; |
| | 0 | 286 | | foreach (var tTypeCombo in typesWithMyAttribute) |
| | | 287 | | { |
| | 0 | 288 | | lock (TypeCacheLock) |
| | | 289 | | { |
| | 0 | 290 | | if (!PreLoadProxy(tTypeCombo.Type, tTypeCombo.Impromptu)) |
| | 0 | 291 | | tSuccess = false; |
| | 0 | 292 | | } |
| | | 293 | | } |
| | 0 | 294 | | return tSuccess; |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | private Type BuildTypeHelper(ModuleBuilder builder, Type contextType, IDictionary<string, Type> informalInterfac |
| | | 298 | | { |
| | | 299 | | |
| | | 300 | | |
| | 2 | 301 | | var tB = builder.DefineType( |
| | 2 | 302 | | $"ActLike_{"InformalInterface"}_{Guid.NewGuid():N}", TypeAttributes.Public | TypeAttributes.Class, |
| | 2 | 303 | | typeof(ActLikeProxy)); |
| | | 304 | | |
| | | 305 | | |
| | | 306 | | |
| | | 307 | | |
| | 8 | 308 | | foreach (var tInterface in informalInterface) |
| | | 309 | | { |
| | | 310 | | |
| | 2 | 311 | | MakePropertyDescribedProperty(builder, tB, contextType, tInterface.Key, tInterface.Value); |
| | | 312 | | |
| | | 313 | | } |
| | 2 | 314 | | var tType = tB.CreateTypeInfo(); |
| | 2 | 315 | | return tType; |
| | | 316 | | } |
| | | 317 | | |
| | | 318 | | private void MakePropertyDescribedProperty(ModuleBuilder builder, TypeBuilder typeBuilder, Type contextType, str |
| | | 319 | | { |
| | | 320 | | |
| | | 321 | | |
| | 2 | 322 | | var tGetName = "get_" + tName; |
| | | 323 | | |
| | 2 | 324 | | var tEmitInfo = new PropertyEmitInfo |
| | 2 | 325 | | { Name = tName, GetName = tGetName, DefaultInterfaceImplementation = true, ContextType = contextType, Re |
| | | 326 | | |
| | | 327 | | |
| | | 328 | | |
| | | 329 | | |
| | 2 | 330 | | MakePropertyHelper(builder, typeBuilder, tEmitInfo); |
| | 2 | 331 | | } |
| | | 332 | | |
| | | 333 | | private static Type DefineCallsiteFieldForMethod(TypeBuilder builder, ActLikeMaker maker, string name, Type retu |
| | | 334 | | { |
| | 587 | 335 | | Type tFuncType = maker.GenerateCallSiteFuncType(argTypes, returnType, info, builder); |
| | 587 | 336 | | Type tReturnType = typeof(CallSite<>).MakeGenericType(tFuncType); |
| | | 337 | | |
| | 587 | 338 | | builder.DefineField(name, tReturnType, FieldAttributes.Static | FieldAttributes.Public); |
| | 587 | 339 | | return tFuncType; |
| | | 340 | | |
| | | 341 | | } |
| | | 342 | | |
| | | 343 | | |
| | | 344 | | internal static Type DefineCallsiteField(TypeBuilder builder, ActLikeMaker maker, string name, Type returnType, |
| | | 345 | | { |
| | 824 | 346 | | Type tFuncType = maker.GenerateCallSiteFuncType(argTypes, returnType); |
| | 824 | 347 | | Type tReturnType = typeof(CallSite<>).MakeGenericType(tFuncType); |
| | | 348 | | |
| | 824 | 349 | | builder.DefineField(name, tReturnType, FieldAttributes.Static | FieldAttributes.Public); |
| | 824 | 350 | | return tFuncType; |
| | | 351 | | |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | private class MethodSigHash |
| | | 355 | | { |
| | | 356 | | public readonly string Name; |
| | | 357 | | public readonly Type[] Parameters; |
| | | 358 | | |
| | 587 | 359 | | public MethodSigHash(MethodInfo info) |
| | | 360 | | { |
| | 587 | 361 | | Name = info.Name; |
| | 587 | 362 | | Parameters = info.GetParameters().Select(it => it.ParameterType).ToArray(); |
| | 587 | 363 | | } |
| | | 364 | | |
| | 0 | 365 | | public MethodSigHash(string name, Type[] parameters) |
| | | 366 | | { |
| | 0 | 367 | | Name = name; |
| | 0 | 368 | | Parameters = parameters; |
| | 0 | 369 | | } |
| | | 370 | | |
| | | 371 | | public bool Equals(MethodSigHash other) |
| | | 372 | | { |
| | 6 | 373 | | if (ReferenceEquals(null, other)) return false; |
| | 6 | 374 | | if (ReferenceEquals(this, other)) return true; |
| | 6 | 375 | | return Equals(other.Name, Name) && StructuralComparisons.StructuralEqualityComparer.Equals(other.Paramet |
| | | 376 | | } |
| | | 377 | | |
| | | 378 | | public override bool Equals(object obj) |
| | | 379 | | { |
| | 6 | 380 | | if (ReferenceEquals(null, obj)) return false; |
| | 6 | 381 | | if (ReferenceEquals(this, obj)) return true; |
| | 6 | 382 | | if (obj.GetType() != typeof(MethodSigHash)) return false; |
| | 6 | 383 | | return Equals((MethodSigHash)obj); |
| | | 384 | | } |
| | | 385 | | |
| | | 386 | | public override int GetHashCode() |
| | | 387 | | { |
| | | 388 | | unchecked |
| | | 389 | | { |
| | 587 | 390 | | return (Name.GetHashCode() * 397) ^ StructuralComparisons.StructuralEqualityComparer.GetHashCode(Par |
| | | 391 | | } |
| | | 392 | | } |
| | | 393 | | } |
| | | 394 | | |
| | | 395 | | private Type BuildTypeHelper(ModuleBuilder builder, Type contextType, params Type[] interfaces) |
| | | 396 | | { |
| | | 397 | | |
| | 71 | 398 | | var tInterfacesMainList = interfaces.Distinct().ToArray(); |
| | 71 | 399 | | var tB = builder.DefineType( |
| | 71 | 400 | | $"ActLike_{tInterfacesMainList.First().Name}_{Guid.NewGuid():N}", TypeAttributes.Public | TypeAttributes |
| | 71 | 401 | | typeof(ActLikeProxy), tInterfacesMainList); |
| | | 402 | | |
| | 71 | 403 | | tB.SetCustomAttribute( |
| | 71 | 404 | | new CustomAttributeBuilder(typeof(ActLikeProxyAttribute).GetConstructor(new[] { typeof(Type).MakeArrayTy |
| | 71 | 405 | | new object[] { interfaces, contextType })); |
| | 71 | 406 | | tB.SetCustomAttribute(new CustomAttributeBuilder(typeof(SerializableAttribute).GetConstructor(Type.EmptyType |
| | | 407 | | |
| | | 408 | | |
| | 71 | 409 | | var tInterfaces = tInterfacesMainList.Concat(tInterfacesMainList.SelectMany(it => it.GetInterfaces())); |
| | | 410 | | |
| | | 411 | | |
| | 71 | 412 | | var tPropertyNameHash = new HashSet<string>(); |
| | 71 | 413 | | var tMethodHashSet = new HashSet<MethodSigHash>(); |
| | | 414 | | |
| | 71 | 415 | | object tAttr = null; |
| | 330 | 416 | | foreach (var tInterface in tInterfaces.Distinct()) |
| | | 417 | | { |
| | | 418 | | |
| | 94 | 419 | | if (tInterface != null && tAttr == null) |
| | | 420 | | { |
| | 94 | 421 | | var tCustomAttributes = tInterface.GetCustomAttributesData(); |
| | 196 | 422 | | foreach (var tCustomAttribute in tCustomAttributes.Where(it => typeof(DefaultMemberAttribute).IsAssi |
| | | 423 | | { |
| | | 424 | | try |
| | | 425 | | { |
| | 4 | 426 | | tB.SetCustomAttribute(GetAttributeBuilder(tCustomAttribute)); |
| | 4 | 427 | | } |
| | 0 | 428 | | catch |
| | | 429 | | { |
| | 0 | 430 | | } //For most proxies not having the same attributes won't really matter, |
| | | 431 | | //but just incase we don't want to stop for some unknown attribute that we can't initialize |
| | | 432 | | } |
| | | 433 | | } |
| | | 434 | | |
| | | 435 | | |
| | | 436 | | |
| | | 437 | | |
| | 94 | 438 | | var tNonRecursive = tInterface.GetCustomAttributes(typeof(NonRecursiveInterfaceAttribute), true).Any(); |
| | | 439 | | |
| | 380 | 440 | | foreach (var tInfo in tInterface.GetProperties(BindingFlags.Public | BindingFlags.Instance)) |
| | | 441 | | { |
| | 96 | 442 | | var tNonRecursiveProp = tNonRecursive || |
| | 96 | 443 | | tInfo.GetCustomAttributes(typeof(NonRecursiveInterfaceAttribute), true).Any( |
| | | 444 | | |
| | 96 | 445 | | MakeProperty(builder, tInfo, tB, contextType, nonRecursive: tNonRecursiveProp, defaultImp: tProperty |
| | | 446 | | } |
| | 1362 | 447 | | foreach (var tInfo in tInterface.GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(it => !it |
| | | 448 | | { |
| | 587 | 449 | | var tNonRecursiveMeth = tNonRecursive || |
| | 587 | 450 | | tInfo.GetCustomAttributes(typeof(NonRecursiveInterfaceAttribute), true).Any( |
| | 587 | 451 | | MakeMethod(builder, tInfo, tB, contextType, nonRecursive: tNonRecursiveMeth, defaultImp: tMethodHash |
| | | 452 | | } |
| | 198 | 453 | | foreach (var tInfo in tInterface.GetEvents(BindingFlags.Public | BindingFlags.Instance).Where(it => !it. |
| | | 454 | | { |
| | 5 | 455 | | MakeEvent(builder, tInfo, tB, contextType, defaultImp: tPropertyNameHash.Add(tInfo.Name)); |
| | | 456 | | } |
| | | 457 | | } |
| | 71 | 458 | | var tType = tB.CreateTypeInfo(); |
| | 71 | 459 | | return tType; |
| | | 460 | | } |
| | | 461 | | |
| | | 462 | | |
| | | 463 | | private IEnumerable<Type> FlattenGenericParameters(Type type) |
| | | 464 | | { |
| | 2059 | 465 | | if (type.IsByRef || type.IsArray || type.IsPointer) |
| | | 466 | | { |
| | 8 | 467 | | return FlattenGenericParameters(type.GetElementType()); |
| | | 468 | | } |
| | | 469 | | |
| | 2051 | 470 | | if (type.IsGenericParameter) |
| | 827 | 471 | | return new[] { type }; |
| | 1224 | 472 | | if (type.ContainsGenericParameters) |
| | | 473 | | { |
| | 266 | 474 | | return type.GetGenericArguments().SelectMany(FlattenGenericParameters); |
| | | 475 | | } |
| | 958 | 476 | | return new Type[] { }; |
| | | 477 | | } |
| | | 478 | | |
| | | 479 | | private Type ReplaceTypeWithGenericBuilder(Type type, IDictionary<Type, GenericTypeParameterBuilder> dict) |
| | | 480 | | { |
| | 2912 | 481 | | var tStartType = type; |
| | | 482 | | Type tReturnType; |
| | 2912 | 483 | | if (type.IsByRef || type.IsArray || type.IsPointer) |
| | | 484 | | { |
| | 3 | 485 | | tStartType = type.GetElementType(); |
| | | 486 | | } |
| | | 487 | | |
| | | 488 | | |
| | 2912 | 489 | | if (tStartType.IsGenericType) |
| | | 490 | | { |
| | 1126 | 491 | | var tArgs = tStartType.GetGenericArguments().Select(it => ReplaceTypeWithGenericBuilder(it, dict)); |
| | | 492 | | |
| | 1126 | 493 | | var tNewType = tStartType.GetGenericTypeDefinition().MakeGenericType(tArgs.ToArray()); |
| | 1126 | 494 | | tReturnType = tNewType; |
| | | 495 | | } |
| | 1786 | 496 | | else if (dict.ContainsKey(tStartType)) |
| | | 497 | | { |
| | 1119 | 498 | | var tNewType = dict[tStartType]; |
| | 1119 | 499 | | var tAttributes = tStartType.GenericParameterAttributes; |
| | 1119 | 500 | | tNewType.SetGenericParameterAttributes(tAttributes); |
| | 2246 | 501 | | foreach (var tConstraint in tStartType.GetGenericParameterConstraints()) |
| | | 502 | | { |
| | 4 | 503 | | if (tConstraint.IsInterface) |
| | 2 | 504 | | tNewType.SetInterfaceConstraints(tConstraint); |
| | | 505 | | else |
| | 2 | 506 | | tNewType.SetBaseTypeConstraint(tConstraint); |
| | | 507 | | } |
| | 1119 | 508 | | tReturnType = tNewType; |
| | | 509 | | } |
| | | 510 | | else |
| | | 511 | | { |
| | 667 | 512 | | tReturnType = tStartType; |
| | | 513 | | } |
| | | 514 | | |
| | 2912 | 515 | | if (type.IsByRef) |
| | | 516 | | { |
| | 3 | 517 | | return tReturnType.MakeByRefType(); |
| | | 518 | | } |
| | | 519 | | |
| | 2909 | 520 | | if (type.IsArray) |
| | | 521 | | { |
| | 0 | 522 | | return tReturnType.MakeArrayType(); |
| | | 523 | | } |
| | | 524 | | |
| | 2909 | 525 | | if (type.IsPointer) |
| | | 526 | | { |
| | 0 | 527 | | return tReturnType.MakePointerType(); |
| | | 528 | | } |
| | | 529 | | |
| | 2909 | 530 | | return tReturnType; |
| | | 531 | | } |
| | | 532 | | |
| | | 533 | | private object CustomAttributeTypeArgument(CustomAttributeTypedArgument argument) |
| | | 534 | | { |
| | 6 | 535 | | if (argument.Value is ReadOnlyCollection<CustomAttributeTypedArgument>) |
| | | 536 | | { |
| | 2 | 537 | | var tValue = argument.Value as ReadOnlyCollection<CustomAttributeTypedArgument>; |
| | 2 | 538 | | return |
| | 2 | 539 | | new ArrayList(tValue.Select(it => it.Value).ToList()).ToArray(argument.ArgumentType.GetElementType() |
| | | 540 | | } |
| | | 541 | | |
| | 4 | 542 | | return argument.Value; |
| | | 543 | | } |
| | | 544 | | |
| | | 545 | | |
| | 1 | 546 | | private static readonly Type[] FuncKinds = |
| | 1 | 547 | | { |
| | 1 | 548 | | typeof(Func<>), typeof(Func<,>), typeof(Func<,,>), typeof(Func<,,,>), typeof(Func<,,,,>), |
| | 1 | 549 | | typeof(Func<,,,,,>), typeof(Func<,,,,,,>), typeof(Func<,,,,,,,>), typeof(Func<,,,,,,,,>), |
| | 1 | 550 | | typeof(Func<,,,,,,,,,>), typeof(Func<,,,,,,,,,,>), typeof(Func<,,,,,,,,,,,>), |
| | 1 | 551 | | typeof(Func<,,,,,,,,,,,,>), typeof(Func<,,,,,,,,,,,,,>), typeof(Func<,,,,,,,,,,,,,,>), |
| | 1 | 552 | | typeof(Func<,,,,,,,,,,,,,,,>), typeof(Func<,,,,,,,,,,,,,,,,>) |
| | 1 | 553 | | }; |
| | | 554 | | |
| | 1 | 555 | | private static readonly Type[] ActionKinds = |
| | 1 | 556 | | { |
| | 1 | 557 | | typeof(Action), typeof(Action<>), typeof(Action<,>), typeof(Action<,,>), typeof(Action<,,,>), |
| | 1 | 558 | | typeof(Action<,,,,>), typeof(Action<,,,,,>), typeof(Action<,,,,,,>), typeof(Action<,,,,,,,>), |
| | 1 | 559 | | typeof(Action<,,,,,,,,>), typeof(Action<,,,,,,,,,>), typeof(Action<,,,,,,,,,,>), |
| | 1 | 560 | | typeof(Action<,,,,,,,,,,,>), typeof(Action<,,,,,,,,,,,,>), typeof(Action<,,,,,,,,,,,,,>), |
| | 1 | 561 | | typeof(Action<,,,,,,,,,,,,,,>), typeof(Action<,,,,,,,,,,,,,,,>) |
| | 1 | 562 | | }; |
| | | 563 | | |
| | | 564 | | /// <summary> |
| | | 565 | | /// The open `Func`/`Action` of that shape. `paramCount` counts the return type for a |
| | | 566 | | /// `Func`, as the caller's list does. |
| | | 567 | | /// </summary> |
| | | 568 | | private static Type GenericDelegateType(int paramCount, bool returnVoid = false) |
| | | 569 | | { |
| | 408 | 570 | | var count = returnVoid ? paramCount : paramCount - 1; |
| | 408 | 571 | | if (count > 16) |
| | 0 | 572 | | throw new ArgumentException( |
| | 0 | 573 | | $"{(returnVoid ? "Action" : "Func")} only handles at most {(returnVoid ? 16 : 17)} parameters", |
| | 0 | 574 | | nameof(paramCount)); |
| | 408 | 575 | | if (count < 0) |
| | 0 | 576 | | throw new ArgumentException( |
| | 0 | 577 | | $"{(returnVoid ? "Action" : "Func")} must have at least {(returnVoid ? 0 : 1)} parameter(s)", |
| | 0 | 578 | | nameof(paramCount)); |
| | | 579 | | |
| | 408 | 580 | | return returnVoid ? ActionKinds[count] : FuncKinds[count]; |
| | | 581 | | } |
| | | 582 | | |
| | | 583 | | private CustomAttributeBuilder GetAttributeBuilder(CustomAttributeData data) |
| | | 584 | | { |
| | 13 | 585 | | var tPropertyInfos = new List<PropertyInfo>(); |
| | 13 | 586 | | var tPropertyValues = new List<object>(); |
| | 13 | 587 | | var tFieldInfos = new List<FieldInfo>(); |
| | 13 | 588 | | var tFieldValues = new List<object>(); |
| | | 589 | | |
| | 13 | 590 | | if (data.NamedArguments != null) |
| | | 591 | | { |
| | 26 | 592 | | foreach (var namedArg in data.NamedArguments) |
| | | 593 | | { |
| | 0 | 594 | | var fi = namedArg.MemberInfo as FieldInfo; |
| | 0 | 595 | | var pi = namedArg.MemberInfo as PropertyInfo; |
| | | 596 | | |
| | 0 | 597 | | if (fi != null) |
| | | 598 | | { |
| | 0 | 599 | | tFieldInfos.Add(fi); |
| | 0 | 600 | | tFieldValues.Add(namedArg.TypedValue.Value); |
| | | 601 | | } |
| | 0 | 602 | | else if (pi != null) |
| | | 603 | | { |
| | 0 | 604 | | tPropertyInfos.Add(pi); |
| | 0 | 605 | | tPropertyValues.Add(namedArg.TypedValue.Value); |
| | | 606 | | } |
| | | 607 | | } |
| | | 608 | | } |
| | | 609 | | |
| | 13 | 610 | | return new CustomAttributeBuilder( |
| | 13 | 611 | | data.Constructor, |
| | 13 | 612 | | data.ConstructorArguments.Select(CustomAttributeTypeArgument).ToArray(), |
| | 13 | 613 | | tPropertyInfos.ToArray(), |
| | 13 | 614 | | tPropertyValues.ToArray(), |
| | 13 | 615 | | tFieldInfos.ToArray(), |
| | 13 | 616 | | tFieldValues.ToArray()); |
| | | 617 | | } |
| | | 618 | | |
| | | 619 | | |
| | | 620 | | private void MakeMethod(ModuleBuilder builder, MethodInfo info, TypeBuilder typeBuilder, Type contextType, bool |
| | | 621 | | { |
| | | 622 | | |
| | | 623 | | |
| | 587 | 624 | | var tEmitInfo = new MethodEmitInfo { Name = info.Name, DefaultInterfaceImplementation = defaultImp, NonRecur |
| | | 625 | | |
| | 587 | 626 | | if (info.GetCustomAttributes(typeof(AliasAttribute), false).FirstOrDefault() is AliasAttribute alias) |
| | | 627 | | { |
| | 1 | 628 | | tEmitInfo.Alias = alias.Name; |
| | | 629 | | } |
| | | 630 | | |
| | 587 | 631 | | var tParamAttri = info.GetParameters(); |
| | 587 | 632 | | Type[] tParamTypes = tParamAttri.Select(it => it.ParameterType).ToArray(); |
| | | 633 | | |
| | | 634 | | |
| | | 635 | | IEnumerable<string> tArgNames; |
| | 587 | 636 | | if (info.GetCustomAttributes(typeof(UseNamedArgumentAttribute), false).Any() || |
| | 587 | 637 | | info.DeclaringType.GetCustomAttributes(typeof(UseNamedArgumentAttribute), false).Any()) |
| | | 638 | | |
| | | 639 | | { |
| | 1 | 640 | | tArgNames = tParamAttri.Select(it => it.Name).ToList(); |
| | | 641 | | } |
| | | 642 | | else |
| | | 643 | | { |
| | 586 | 644 | | var tParam = tParamAttri.Zip(Enumerable.Range(0, tParamTypes.Count()), (p, i) => new { i, p }) |
| | 586 | 645 | | .FirstOrDefault(it => it.p.GetCustomAttributes(typeof(UseNamedArgumentAttribute), false).Any()); |
| | | 646 | | |
| | 586 | 647 | | tArgNames = tParam == null |
| | 586 | 648 | | ? Enumerable.Repeat(default(string), tParamTypes.Length) |
| | 586 | 649 | | : Enumerable.Repeat(default(string), tParam.i).Concat(tParamAttri.Skip(Math.Min(tParam.i - 1, 0)).Se |
| | | 650 | | } |
| | | 651 | | |
| | | 652 | | |
| | 587 | 653 | | var tReturnType = typeof(void); |
| | 587 | 654 | | if (info.ReturnParameter != null) |
| | 587 | 655 | | tReturnType = info.ReturnParameter.ParameterType; |
| | | 656 | | |
| | | 657 | | |
| | 587 | 658 | | var tCallSiteName = tEmitInfo.CallSiteName; |
| | 587 | 659 | | var tCStp = DefineBuilderForCallSite(builder, tCallSiteName); |
| | | 660 | | |
| | | 661 | | |
| | | 662 | | |
| | 587 | 663 | | var tReplacedTypes = GetParamTypes(tCStp, info); |
| | 587 | 664 | | if (tReplacedTypes != null) |
| | | 665 | | { |
| | 159 | 666 | | tReturnType = tReplacedTypes.Item1; |
| | 159 | 667 | | tParamTypes = tReplacedTypes.Item2; |
| | | 668 | | } |
| | | 669 | | |
| | 587 | 670 | | var tConvert = tEmitInfo.CallSiteConvertName; |
| | 587 | 671 | | Type tConvertFuncType = null; |
| | 587 | 672 | | if (tReturnType != typeof(void)) |
| | | 673 | | { |
| | 569 | 674 | | tConvertFuncType = DefineCallsiteField(tCStp, this, tConvert, tReturnType); |
| | | 675 | | } |
| | | 676 | | |
| | 587 | 677 | | var tInvokeMethod = tEmitInfo.CallSiteInvokeName; |
| | 587 | 678 | | var tInvokeFuncType = DefineCallsiteFieldForMethod(tCStp, this, tInvokeMethod, tReturnType != typeof(void) ? |
| | | 679 | | |
| | | 680 | | |
| | | 681 | | |
| | | 682 | | |
| | 587 | 683 | | Type tCallSite = tCStp.CreateTypeInfo(); |
| | | 684 | | |
| | 587 | 685 | | var tPublicPrivate = MethodAttributes.Public; |
| | 587 | 686 | | var tPrefixName = tEmitInfo.Name; |
| | 587 | 687 | | if (!tEmitInfo.DefaultInterfaceImplementation) |
| | | 688 | | { |
| | 6 | 689 | | tPrefixName = String.Format("{0}.{1}", info.DeclaringType.FullName, tPrefixName); |
| | | 690 | | |
| | 6 | 691 | | tPublicPrivate = MethodAttributes.Private; |
| | | 692 | | } |
| | | 693 | | |
| | | 694 | | |
| | 587 | 695 | | var tMethodBuilder = typeBuilder.DefineMethod(tPrefixName, |
| | 587 | 696 | | tPublicPrivate | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.Final | Method |
| | | 697 | | |
| | | 698 | | |
| | | 699 | | |
| | 587 | 700 | | tReplacedTypes = GetParamTypes(tMethodBuilder, info); |
| | 587 | 701 | | var tReducedParams = tParamTypes.Select(ReduceToElementType).ToArray(); |
| | 587 | 702 | | if (tReplacedTypes != null) |
| | | 703 | | { |
| | 159 | 704 | | tReturnType = tReplacedTypes.Item1; |
| | 159 | 705 | | tParamTypes = tReplacedTypes.Item2; |
| | | 706 | | |
| | 159 | 707 | | tReducedParams = tParamTypes.Select(ReduceToElementType).ToArray(); |
| | 159 | 708 | | var tGenericParams = tMethodBuilder.GetGenericArguments(); |
| | 159 | 709 | | tCallSite = tCallSite.GetGenericTypeDefinition().MakeGenericType(tGenericParams); |
| | 159 | 710 | | if (tConvertFuncType != null) |
| | 159 | 711 | | tConvertFuncType = UpdateCallsiteFuncType(tConvertFuncType, tReturnType); |
| | 159 | 712 | | tInvokeFuncType = UpdateCallsiteFuncType(tInvokeFuncType, tReturnType != typeof(void) ? typeof(object) : |
| | | 713 | | } |
| | | 714 | | |
| | 587 | 715 | | tMethodBuilder.SetReturnType(tReturnType); |
| | 587 | 716 | | tMethodBuilder.SetParameters(tParamTypes); |
| | | 717 | | |
| | 2548 | 718 | | foreach (var tParam in info.GetParameters()) |
| | | 719 | | { |
| | 687 | 720 | | var tParamBuilder = tMethodBuilder.DefineParameter(tParam.Position + 1, AttributesForParam(tParam), tPar |
| | | 721 | | |
| | | 722 | | |
| | 687 | 723 | | var tCustomAttributes = tParam.GetCustomAttributesData(); |
| | 1392 | 724 | | foreach (var tCustomAttribute in tCustomAttributes) |
| | | 725 | | { |
| | | 726 | | try |
| | | 727 | | { |
| | 9 | 728 | | tParamBuilder.SetCustomAttribute(GetAttributeBuilder(tCustomAttribute)); |
| | 9 | 729 | | } |
| | 0 | 730 | | catch { }//For most proxies not having the same attributes won't really matter, |
| | | 731 | | //but just incase we don't want to stop for some unknown attribute that we can't initialize |
| | | 732 | | } |
| | | 733 | | |
| | | 734 | | |
| | | 735 | | } |
| | | 736 | | |
| | 587 | 737 | | if (!tEmitInfo.DefaultInterfaceImplementation) |
| | | 738 | | { |
| | 6 | 739 | | typeBuilder.DefineMethodOverride(tMethodBuilder, info); |
| | | 740 | | } |
| | 587 | 741 | | tEmitInfo.ResolvedParamTypes = tReducedParams; |
| | 587 | 742 | | tEmitInfo.ResolveReturnType = tReturnType; |
| | 587 | 743 | | tEmitInfo.CallSiteType = tCallSite; |
| | 587 | 744 | | tEmitInfo.ContextType = contextType; |
| | 587 | 745 | | tEmitInfo.CallSiteConvertFuncType = tConvertFuncType; |
| | 587 | 746 | | tEmitInfo.CallSiteInvokeFuncType = tInvokeFuncType; |
| | 587 | 747 | | tEmitInfo.ArgNames = tArgNames; |
| | 587 | 748 | | EmitMethodBody(tMethodBuilder, tParamAttri, tEmitInfo); |
| | 587 | 749 | | } |
| | | 750 | | |
| | | 751 | | private TypeBuilder DefineBuilderForCallSite(ModuleBuilder builder, string tCallSiteInvokeName) |
| | | 752 | | { |
| | 690 | 753 | | return builder.DefineType(tCallSiteInvokeName, |
| | 690 | 754 | | |
| | 690 | 755 | | TypeAttributes.NotPublic |
| | 690 | 756 | | | TypeAttributes.Sealed |
| | 690 | 757 | | | TypeAttributes.AutoClass |
| | 690 | 758 | | | TypeAttributes.BeforeFieldInit |
| | 690 | 759 | | | TypeAttributes.Abstract |
| | 690 | 760 | | ); |
| | | 761 | | } |
| | | 762 | | |
| | | 763 | | private Tuple<Type, Type[]> GetParamTypes(dynamic builder, MethodInfo info) |
| | | 764 | | { |
| | 1178 | 765 | | if (info == null) |
| | 0 | 766 | | return null; |
| | | 767 | | |
| | | 768 | | |
| | 1178 | 769 | | var paramTypes = info.GetParameters().Select(it => it.ParameterType).ToArray(); |
| | 1178 | 770 | | var returnType = typeof(void); |
| | 1178 | 771 | | if (info.ReturnParameter != null) |
| | 1178 | 772 | | returnType = info.ReturnParameter.ParameterType; |
| | | 773 | | |
| | 1178 | 774 | | var tGenericParams = info.GetGenericArguments() |
| | 1178 | 775 | | .SelectMany(FlattenGenericParameters) |
| | 1178 | 776 | | .Distinct().ToDictionary(it => it.GenericParameterPosition, it => new { Type = it, Gen = default(Generic |
| | 1178 | 777 | | var tParams = tGenericParams; |
| | 1178 | 778 | | var tReturnParameters = FlattenGenericParameters(returnType).Where(it => !tParams.ContainsKey(it.GenericPara |
| | 2356 | 779 | | foreach (var tReParm in tReturnParameters) |
| | 0 | 780 | | tGenericParams.Add(tReParm.GenericParameterPosition, new { Type = tReParm, Gen = default(GenericTypePara |
| | 1178 | 781 | | var tGenParams = tGenericParams.OrderBy(it => it.Key).Select(it => it.Value.Type.Name); |
| | 1178 | 782 | | if (tGenParams.Any()) |
| | | 783 | | { |
| | 319 | 784 | | GenericTypeParameterBuilder[] tBuilders = builder.DefineGenericParameters(tGenParams.ToArray()); |
| | 319 | 785 | | var tDict = tGenericParams.ToDictionary(param => param.Value.Type, param => tBuilders[param.Key]); |
| | | 786 | | |
| | 319 | 787 | | returnType = ReplaceTypeWithGenericBuilder(returnType, tDict); |
| | 319 | 788 | | if (tDict.ContainsKey(returnType)) |
| | | 789 | | { |
| | 0 | 790 | | returnType = tDict[returnType]; |
| | | 791 | | } |
| | 319 | 792 | | paramTypes = paramTypes.Select(it => ReplaceTypeWithGenericBuilder(it, tDict)).ToArray(); |
| | 319 | 793 | | return Tuple.Create(returnType, paramTypes); |
| | | 794 | | } |
| | 859 | 795 | | return null; |
| | | 796 | | } |
| | | 797 | | |
| | | 798 | | |
| | | 799 | | |
| | | 800 | | |
| | | 801 | | private void EmitMethodBody( |
| | | 802 | | MethodBuilder methodBuilder, |
| | | 803 | | ParameterInfo[] paramInfo, |
| | | 804 | | MethodEmitInfo emitInfo |
| | | 805 | | ) |
| | | 806 | | { |
| | 587 | 807 | | var tIlGen = methodBuilder.GetILGenerator(); |
| | | 808 | | |
| | 587 | 809 | | var tConvertField = emitInfo.CallSiteType.GetFieldEvenIfGeneric(emitInfo.CallSiteConvertName); |
| | 587 | 810 | | if (emitInfo.ResolveReturnType != typeof(void)) |
| | | 811 | | { |
| | | 812 | | |
| | | 813 | | |
| | 569 | 814 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tConvertField))) |
| | | 815 | | { |
| | 569 | 816 | | tIlGen.EmitDynamicConvertBinder(CSharpBinderFlags.None, emitInfo.ResolveReturnType, emitInfo.Context |
| | 569 | 817 | | tIlGen.EmitCallsiteCreate(emitInfo.CallSiteConvertFuncType); |
| | 569 | 818 | | tIlGen.Emit(OpCodes.Stsfld, tConvertField); |
| | 569 | 819 | | } |
| | | 820 | | } |
| | | 821 | | |
| | 587 | 822 | | var tInvokeField = emitInfo.CallSiteType.GetFieldEvenIfGeneric(emitInfo.CallSiteInvokeName); |
| | | 823 | | |
| | 587 | 824 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tInvokeField))) |
| | | 825 | | { |
| | 587 | 826 | | tIlGen.EmitDynamicMethodInvokeBinder( |
| | 587 | 827 | | emitInfo.ResolveReturnType == typeof(void) ? CSharpBinderFlags.ResultDiscarded : CSharpBinderFlags.N |
| | 587 | 828 | | emitInfo.Alias ?? emitInfo.Name, |
| | 587 | 829 | | methodBuilder.GetGenericArguments(), |
| | 587 | 830 | | emitInfo.ContextType, |
| | 587 | 831 | | paramInfo, |
| | 587 | 832 | | emitInfo.ArgNames); |
| | 587 | 833 | | tIlGen.EmitCallsiteCreate(emitInfo.CallSiteInvokeFuncType); |
| | 587 | 834 | | tIlGen.Emit(OpCodes.Stsfld, tInvokeField); |
| | 587 | 835 | | } |
| | | 836 | | |
| | | 837 | | //If it's an interface and not nonrecursive this will be true |
| | 587 | 838 | | var tRecurse = emitInfo.ResolveReturnType.IsInterface && !emitInfo.NonRecursive; |
| | | 839 | | |
| | 587 | 840 | | if (emitInfo.ResolveReturnType != typeof(void) && !tRecurse) |
| | | 841 | | { |
| | 348 | 842 | | tIlGen.Emit(OpCodes.Ldsfld, tConvertField); |
| | 348 | 843 | | tIlGen.Emit(OpCodes.Ldfld, typeof(CallSite<>).MakeGenericType(emitInfo.CallSiteConvertFuncType).GetField |
| | 348 | 844 | | tIlGen.Emit(OpCodes.Ldsfld, tConvertField); |
| | | 845 | | } |
| | | 846 | | |
| | 587 | 847 | | tIlGen.Emit(OpCodes.Ldsfld, tInvokeField); |
| | 587 | 848 | | tIlGen.Emit(OpCodes.Ldfld, typeof(CallSite<>).MakeGenericType(emitInfo.CallSiteInvokeFuncType).GetFieldEvenI |
| | 587 | 849 | | tIlGen.Emit(OpCodes.Ldsfld, tInvokeField); |
| | 587 | 850 | | tIlGen.Emit(OpCodes.Ldarg_0); |
| | 587 | 851 | | tIlGen.Emit(OpCodes.Callvirt, typeof(IActLikeProxy).GetProperty(nameof(IActLikeProxy.Original)).GetGetMethod |
| | 2548 | 852 | | for (var i = 1; i <= emitInfo.ResolvedParamTypes.Length; i++) |
| | | 853 | | { |
| | | 854 | | |
| | 687 | 855 | | tIlGen.EmitLoadArgument(i); |
| | | 856 | | } |
| | 587 | 857 | | tIlGen.EmitCallInvokeFunc(emitInfo.CallSiteInvokeFuncType); |
| | 587 | 858 | | if (emitInfo.ResolveReturnType != typeof(void) && !tRecurse) |
| | | 859 | | { |
| | 348 | 860 | | tIlGen.EmitCallInvokeFunc(emitInfo.CallSiteConvertFuncType); |
| | | 861 | | } |
| | | 862 | | |
| | | 863 | | //If we are recursing, try actlike |
| | 587 | 864 | | if (tRecurse) |
| | | 865 | | { |
| | 221 | 866 | | var tReturnLocal = tIlGen.DeclareLocal(typeof(object)); |
| | 221 | 867 | | tIlGen.EmitStoreLocation(tReturnLocal.LocalIndex); |
| | 221 | 868 | | using (tIlGen.EmitBranchFalse(gen => gen.EmitLoadLocation(tReturnLocal.LocalIndex))) |
| | | 869 | | { |
| | 221 | 870 | | tIlGen.EmitLoadLocation(tReturnLocal.LocalIndex); |
| | 221 | 871 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Isinst, emitInfo.ResolveReturnType))) |
| | | 872 | | { |
| | 221 | 873 | | tIlGen.Emit(OpCodes.Ldarg_0); |
| | 221 | 874 | | tIlGen.Emit(OpCodes.Callvirt, typeof(IActLikeProxy).GetProperty(nameof(IActLikeProxy.Maker)).Get |
| | 221 | 875 | | tIlGen.EmitLoadLocation(tReturnLocal.LocalIndex); |
| | 221 | 876 | | tIlGen.Emit(OpCodes.Call, ActLikeRec.MakeGenericMethod(emitInfo.ResolveReturnType)); |
| | 221 | 877 | | tIlGen.Emit(OpCodes.Ret); |
| | 221 | 878 | | } |
| | | 879 | | } |
| | | 880 | | |
| | 221 | 881 | | tIlGen.Emit(OpCodes.Ldsfld, tConvertField); |
| | 221 | 882 | | tIlGen.Emit(OpCodes.Ldfld, typeof(CallSite<>).MakeGenericType(emitInfo.CallSiteConvertFuncType).GetField |
| | 221 | 883 | | tIlGen.Emit(OpCodes.Ldsfld, tConvertField); |
| | 221 | 884 | | tIlGen.EmitLoadLocation(tReturnLocal.LocalIndex); |
| | 221 | 885 | | tIlGen.EmitCallInvokeFunc(emitInfo.CallSiteConvertFuncType); |
| | | 886 | | } |
| | | 887 | | |
| | | 888 | | |
| | 587 | 889 | | tIlGen.Emit(OpCodes.Ret); |
| | 587 | 890 | | } |
| | | 891 | | |
| | | 892 | | |
| | | 893 | | private void MakeProperty(ModuleBuilder builder, PropertyInfo info, TypeBuilder typeBuilder, Type contextType, b |
| | | 894 | | { |
| | 96 | 895 | | var tEmitInfo = new PropertyEmitInfo() |
| | 96 | 896 | | { |
| | 96 | 897 | | Name = info.Name, |
| | 96 | 898 | | ResolveReturnType = info.PropertyType, |
| | 96 | 899 | | GetName = info.CanRead ? info.GetGetMethod().Name : null, // q: getter may be missing! |
| | 96 | 900 | | SetName = info.CanWrite ? info.GetSetMethod().Name : null, // q: setter may be missing! |
| | 96 | 901 | | ContextType = contextType, |
| | 96 | 902 | | DefaultInterfaceImplementation = defaultImp, |
| | 96 | 903 | | NonRecursive = nonRecursive |
| | 96 | 904 | | }; |
| | | 905 | | |
| | 96 | 906 | | if (info.GetCustomAttributes(typeof(AliasAttribute), false).FirstOrDefault() is AliasAttribute alias) |
| | | 907 | | { |
| | 1 | 908 | | tEmitInfo.Alias = alias.Name; |
| | | 909 | | } |
| | | 910 | | |
| | 96 | 911 | | MakePropertyHelper(builder, typeBuilder, tEmitInfo, info); |
| | 96 | 912 | | } |
| | | 913 | | |
| | | 914 | | private class PropertyEmitInfo : EmitInfo |
| | | 915 | | { |
| | 103 | 916 | | public PropertyEmitInfo() |
| | | 917 | | { |
| | 103 | 918 | | CallSiteConvertName = "Convert_Get"; |
| | 103 | 919 | | CallSiteInvokeGetName = "Invoke_Get"; |
| | 103 | 920 | | CallSiteInvokeSetName = "Invoke_Set"; |
| | | 921 | | |
| | 103 | 922 | | } |
| | | 923 | | public string GetName { get; set; } |
| | | 924 | | public string SetName { get; set; } |
| | | 925 | | |
| | | 926 | | public string CallSiteInvokeSetName { get; protected set; } |
| | | 927 | | public string CallSiteInvokeGetName { get; protected set; } |
| | | 928 | | public string CallSiteConvertName { get; protected set; } |
| | | 929 | | |
| | | 930 | | public Type[] ResolvedIndexParamTypes { get; set; } |
| | | 931 | | public Type CallSiteConvertFuncType { get; set; } |
| | | 932 | | public Type CallSiteInvokeGetFuncType { get; set; } |
| | | 933 | | public Type CallSiteInvokeSetFuncType { get; set; } |
| | | 934 | | } |
| | | 935 | | |
| | | 936 | | private class EmitEventInfo : PropertyEmitInfo |
| | | 937 | | { |
| | | 938 | | public string CallSiteIsEventName { get; protected set; } |
| | | 939 | | public string CallSiteAddAssignName { get; protected set; } |
| | | 940 | | public string CallSiteSubtractAssignName { get; protected set; } |
| | | 941 | | public string CallSiteAddName { get; protected set; } |
| | | 942 | | public string CallSiteRemoveName { get; protected set; } |
| | | 943 | | |
| | | 944 | | |
| | | 945 | | public Type CallSiteIsEventFuncType { get; set; } |
| | | 946 | | public Type CallSiteAddAssignFuncType { get; set; } |
| | | 947 | | public Type CallSiteSubtractAssignFuncType { get; set; } |
| | | 948 | | public Type CallSiteAddFuncType { get; set; } |
| | | 949 | | public Type CallSiteRemoveFuncType { get; set; } |
| | | 950 | | public Type[] ResolvedAddParamTypes { get; set; } |
| | | 951 | | public Type[] ResolvedRemoveParamTypes { get; set; } |
| | | 952 | | |
| | 5 | 953 | | public EmitEventInfo() |
| | | 954 | | { |
| | 5 | 955 | | CallSiteIsEventName = "Invoke_IsEvent"; |
| | 5 | 956 | | CallSiteAddAssignName = "Invoke_AddAssign"; |
| | 5 | 957 | | CallSiteSubtractAssignName = "Invoke_SubtractAssign"; |
| | 5 | 958 | | CallSiteAddName = "Invoke_Add"; |
| | 5 | 959 | | CallSiteRemoveName = "Invoke_Remove"; |
| | 5 | 960 | | } |
| | | 961 | | } |
| | | 962 | | |
| | | 963 | | private void MakeEvent(ModuleBuilder builder, EventInfo info, TypeBuilder typeBuilder, Type contextType, bool de |
| | | 964 | | { |
| | 5 | 965 | | var tEmitInfo = new EmitEventInfo |
| | 5 | 966 | | { |
| | 5 | 967 | | Name = info.Name, |
| | 5 | 968 | | ContextType = contextType, |
| | 5 | 969 | | DefaultInterfaceImplementation = defaultImp |
| | 5 | 970 | | }; |
| | 5 | 971 | | if (info.GetCustomAttributes(typeof(AliasAttribute), false).FirstOrDefault() is AliasAttribute alias) |
| | | 972 | | { |
| | 1 | 973 | | tEmitInfo.Alias = alias.Name; |
| | | 974 | | } |
| | | 975 | | |
| | 5 | 976 | | var tAddMethod = info.GetAddMethod(); |
| | 5 | 977 | | var tRemoveMethod = info.GetRemoveMethod(); |
| | 5 | 978 | | tEmitInfo.ResolveReturnType = info.EventHandlerType; |
| | | 979 | | |
| | | 980 | | |
| | 5 | 981 | | var tCStp = DefineBuilderForCallSite(builder, tEmitInfo.CallSiteName); |
| | | 982 | | |
| | | 983 | | |
| | 5 | 984 | | tEmitInfo.CallSiteIsEventFuncType = DefineCallsiteField(tCStp, this, tEmitInfo.CallSiteIsEventName, typeof(b |
| | | 985 | | |
| | | 986 | | |
| | 5 | 987 | | tEmitInfo.CallSiteAddAssignFuncType = DefineCallsiteField(tCStp, this, tEmitInfo.CallSiteAddAssignName, type |
| | | 988 | | |
| | 5 | 989 | | tEmitInfo.CallSiteSubtractAssignFuncType = DefineCallsiteField(tCStp, this, tEmitInfo.CallSiteSubtractAssign |
| | | 990 | | |
| | 5 | 991 | | tEmitInfo.ResolvedAddParamTypes = tRemoveMethod.GetParameters().Select(it => it.ParameterType).ToArray(); |
| | 5 | 992 | | tEmitInfo.CallSiteAddFuncType = DefineCallsiteField(tCStp, this, tEmitInfo.CallSiteAddName, typeof(object), |
| | | 993 | | |
| | 5 | 994 | | tEmitInfo.ResolvedRemoveParamTypes = tRemoveMethod.GetParameters().Select(it => it.ParameterType).ToArray(); |
| | 5 | 995 | | tEmitInfo.CallSiteRemoveFuncType = DefineCallsiteField(tCStp, this, tEmitInfo.CallSiteRemoveName, typeof(obj |
| | | 996 | | |
| | 5 | 997 | | tEmitInfo.CallSiteInvokeGetFuncType = DefineCallsiteField(tCStp, this, tEmitInfo.CallSiteInvokeGetName, type |
| | | 998 | | |
| | 5 | 999 | | tEmitInfo.CallSiteInvokeSetFuncType = DefineCallsiteField(tCStp, this, tEmitInfo.CallSiteInvokeSetName, type |
| | | 1000 | | |
| | 5 | 1001 | | tEmitInfo.CallSiteType = tCStp.CreateTypeInfo(); |
| | | 1002 | | |
| | 5 | 1003 | | var tMp = typeBuilder.DefineEvent(info.Name, EventAttributes.None, tEmitInfo.ResolveReturnType); |
| | | 1004 | | |
| | | 1005 | | |
| | 5 | 1006 | | var tSetField = tEmitInfo.CallSiteType.GetFieldEvenIfGeneric(tEmitInfo.CallSiteInvokeSetName); |
| | 5 | 1007 | | var tGetField = tEmitInfo.CallSiteType.GetFieldEvenIfGeneric(tEmitInfo.CallSiteInvokeGetName); |
| | 5 | 1008 | | var tIsEventField = tEmitInfo.CallSiteType.GetFieldEvenIfGeneric(tEmitInfo.CallSiteIsEventName); |
| | | 1009 | | |
| | 5 | 1010 | | var tPublicPrivate = MethodAttributes.Public; |
| | 5 | 1011 | | if (!tEmitInfo.DefaultInterfaceImplementation) |
| | | 1012 | | { |
| | 1 | 1013 | | tPublicPrivate = MethodAttributes.Private; |
| | | 1014 | | } |
| | | 1015 | | |
| | | 1016 | | //AddMethod |
| | 5 | 1017 | | var tAddPrefixName = tAddMethod.Name; |
| | 5 | 1018 | | if (!tEmitInfo.DefaultInterfaceImplementation) |
| | | 1019 | | { |
| | 1 | 1020 | | tAddPrefixName = $"{info.DeclaringType.FullName}.{tAddPrefixName}"; |
| | | 1021 | | } |
| | | 1022 | | |
| | 5 | 1023 | | var tAddBuilder = typeBuilder.DefineMethod(tAddPrefixName, |
| | 5 | 1024 | | tPublicPrivate | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Virtual | |
| | 5 | 1025 | | typeof(void), |
| | 5 | 1026 | | tEmitInfo.ResolvedAddParamTypes); |
| | | 1027 | | |
| | 5 | 1028 | | if (!tEmitInfo.DefaultInterfaceImplementation) |
| | | 1029 | | { |
| | 1 | 1030 | | typeBuilder.DefineMethodOverride(tAddBuilder, info.GetAddMethod()); |
| | | 1031 | | } |
| | | 1032 | | |
| | 20 | 1033 | | foreach (var tParam in tAddMethod.GetParameters()) |
| | | 1034 | | { |
| | 5 | 1035 | | tAddBuilder.DefineParameter(tParam.Position + 1, AttributesForParam(tParam), tParam.Name); |
| | | 1036 | | } |
| | | 1037 | | |
| | 5 | 1038 | | EmitAddEvent(tMp, tAddBuilder, info, tAddMethod, tGetField, tSetField, tIsEventField, tEmitInfo); |
| | | 1039 | | |
| | | 1040 | | //Remove Method |
| | 5 | 1041 | | var tRemovePrefixName = tRemoveMethod.Name; |
| | 5 | 1042 | | if (!tEmitInfo.DefaultInterfaceImplementation) |
| | | 1043 | | { |
| | 1 | 1044 | | tRemovePrefixName = $"{info.DeclaringType.FullName}.{tRemovePrefixName}"; |
| | | 1045 | | |
| | | 1046 | | } |
| | 5 | 1047 | | var tRemoveBuilder = typeBuilder.DefineMethod(tRemovePrefixName, |
| | 5 | 1048 | | tPublicPrivate |
| | 5 | 1049 | | | MethodAttributes.SpecialName |
| | 5 | 1050 | | | MethodAttributes.HideBySig |
| | 5 | 1051 | | | MethodAttributes.Virtual |
| | 5 | 1052 | | | MethodAttributes.Final |
| | 5 | 1053 | | | MethodAttributes.NewSlot, |
| | 5 | 1054 | | typeof(void), |
| | 5 | 1055 | | tEmitInfo.ResolvedAddParamTypes); |
| | 5 | 1056 | | if (!tEmitInfo.DefaultInterfaceImplementation) |
| | | 1057 | | { |
| | 1 | 1058 | | typeBuilder.DefineMethodOverride(tRemoveBuilder, info.GetRemoveMethod()); |
| | | 1059 | | } |
| | | 1060 | | |
| | 20 | 1061 | | foreach (var tParam in tRemoveMethod.GetParameters()) |
| | | 1062 | | { |
| | 5 | 1063 | | tRemoveBuilder.DefineParameter(tParam.Position + 1, AttributesForParam(tParam), tParam.Name); |
| | | 1064 | | } |
| | | 1065 | | |
| | | 1066 | | |
| | 5 | 1067 | | EmitRemoveEvent(tMp, tRemoveBuilder, info, tRemoveMethod, tGetField, tSetField, tIsEventField, tEmitInfo); |
| | 5 | 1068 | | } |
| | | 1069 | | |
| | | 1070 | | private void EmitRemoveEvent(EventBuilder tMp, MethodBuilder tRemoveBuilder, EventInfo info, MethodInfo tRemoveM |
| | | 1071 | | FieldInfo tGetField, FieldInfo tSetField, FieldInfo tIsEventField, |
| | | 1072 | | EmitEventInfo tEmitInfo) |
| | | 1073 | | { |
| | 5 | 1074 | | var tIlGen = tRemoveBuilder.GetILGenerator(); |
| | | 1075 | | |
| | | 1076 | | |
| | 5 | 1077 | | using (tIlGen.EmitBranchTrue(load => load.Emit(OpCodes.Ldsfld, tIsEventField))) |
| | | 1078 | | { |
| | 5 | 1079 | | tIlGen.EmitDynamicIsEventBinder(CSharpBinderFlags.None, tEmitInfo.Alias ?? tEmitInfo.Name, tEmitInfo.Con |
| | 5 | 1080 | | tIlGen.EmitCallsiteCreate(tEmitInfo.CallSiteIsEventFuncType); |
| | 5 | 1081 | | tIlGen.Emit(OpCodes.Stsfld, tIsEventField); |
| | 5 | 1082 | | } |
| | | 1083 | | |
| | 5 | 1084 | | using (tIlGen.EmitBranchTrue( |
| | 5 | 1085 | | load => load.EmitInvocation( |
| | 5 | 1086 | | target => target.EmitInvocation( |
| | 5 | 1087 | | t => t.Emit(OpCodes.Ldsfld, tIsEventField), |
| | 5 | 1088 | | i => i.Emit(OpCodes.Ldfld, tIsEventField.FieldType.GetFieldEvenIfGeneric("Target")) |
| | 5 | 1089 | | ), |
| | 5 | 1090 | | invoke => invoke.EmitCallInvokeFunc(tEmitInfo.CallSiteIsEventFuncType), |
| | 5 | 1091 | | param => param.Emit(OpCodes.Ldsfld, tIsEventField), |
| | 5 | 1092 | | param => param.EmitInvocation( |
| | 5 | 1093 | | t => t.Emit(OpCodes.Ldarg_0), |
| | 5 | 1094 | | i => i.Emit(OpCodes.Callvirt, typeof(IActLikeProxy).GetProperty("Original").GetGetMethod( |
| | 5 | 1095 | | ) |
| | 5 | 1096 | | ) |
| | 5 | 1097 | | ) |
| | 5 | 1098 | | ) //if IsEvent Not True |
| | | 1099 | | { |
| | 5 | 1100 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tSetField))) |
| | | 1101 | | { |
| | 5 | 1102 | | tIlGen.EmitDynamicSetBinderDynamicParams(CSharpBinderFlags.ValueFromCompoundAssignment, tEmitInfo.Al |
| | 5 | 1103 | | tEmitInfo.ContextType, tEmitInfo.ResolveReturnType); |
| | 5 | 1104 | | tIlGen.EmitCallsiteCreate(tEmitInfo.CallSiteInvokeSetFuncType); |
| | 5 | 1105 | | tIlGen.Emit(OpCodes.Stsfld, tSetField); |
| | 5 | 1106 | | } |
| | | 1107 | | |
| | 5 | 1108 | | var tSubrtractAssignField = tEmitInfo.CallSiteType.GetFieldEvenIfGeneric(tEmitInfo.CallSiteSubtractAssig |
| | | 1109 | | |
| | 5 | 1110 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tSubrtractAssignField))) |
| | | 1111 | | { |
| | 5 | 1112 | | tIlGen.EmitDynamicBinaryOpBinder(CSharpBinderFlags.None, ExpressionType.SubtractAssign, |
| | 5 | 1113 | | tEmitInfo.ContextType, tEmitInfo.ResolveReturnType); |
| | 5 | 1114 | | tIlGen.EmitCallsiteCreate(tEmitInfo.CallSiteSubtractAssignFuncType); |
| | 5 | 1115 | | tIlGen.Emit(OpCodes.Stsfld, tSubrtractAssignField); |
| | 5 | 1116 | | } |
| | | 1117 | | |
| | | 1118 | | |
| | 5 | 1119 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tGetField))) |
| | | 1120 | | { |
| | 5 | 1121 | | tIlGen.EmitDynamicGetBinder(CSharpBinderFlags.None, tEmitInfo.Alias ?? tEmitInfo.Name, tEmitInfo.Con |
| | 5 | 1122 | | tIlGen.EmitCallsiteCreate(tEmitInfo.CallSiteInvokeGetFuncType); |
| | 5 | 1123 | | tIlGen.Emit(OpCodes.Stsfld, tGetField); |
| | 5 | 1124 | | } |
| | | 1125 | | |
| | 5 | 1126 | | tIlGen.Emit(OpCodes.Ldsfld, tSetField); |
| | 5 | 1127 | | tIlGen.Emit(OpCodes.Ldfld, tSetField.FieldType.GetFieldEvenIfGeneric("Target")); |
| | 5 | 1128 | | tIlGen.Emit(OpCodes.Ldsfld, tSetField); |
| | 5 | 1129 | | tIlGen.Emit(OpCodes.Ldarg_0); |
| | 5 | 1130 | | tIlGen.Emit(OpCodes.Callvirt, typeof(IActLikeProxy).GetProperty(nameof(IActLikeProxy.Original)).GetGetMe |
| | | 1131 | | |
| | 5 | 1132 | | tIlGen.Emit(OpCodes.Ldsfld, tSubrtractAssignField); |
| | 5 | 1133 | | tIlGen.Emit(OpCodes.Ldfld, tSubrtractAssignField.FieldType.GetFieldEvenIfGeneric("Target")); |
| | 5 | 1134 | | tIlGen.Emit(OpCodes.Ldsfld, tSubrtractAssignField); |
| | | 1135 | | |
| | 5 | 1136 | | tIlGen.Emit(OpCodes.Ldsfld, tGetField); |
| | 5 | 1137 | | tIlGen.Emit(OpCodes.Ldfld, tGetField.FieldType.GetFieldEvenIfGeneric("Target")); |
| | 5 | 1138 | | tIlGen.Emit(OpCodes.Ldsfld, tGetField); |
| | 5 | 1139 | | tIlGen.Emit(OpCodes.Ldarg_0); |
| | 5 | 1140 | | tIlGen.Emit(OpCodes.Callvirt, typeof(IActLikeProxy).GetProperty(nameof(IActLikeProxy.Original)).GetGetMe |
| | | 1141 | | |
| | 5 | 1142 | | tIlGen.EmitCallInvokeFunc(tEmitInfo.CallSiteInvokeGetFuncType); |
| | | 1143 | | |
| | 5 | 1144 | | tIlGen.Emit(OpCodes.Ldarg_1); |
| | 5 | 1145 | | tIlGen.EmitCallInvokeFunc(tEmitInfo.CallSiteSubtractAssignFuncType); |
| | | 1146 | | |
| | 5 | 1147 | | tIlGen.EmitCallInvokeFunc(tEmitInfo.CallSiteInvokeSetFuncType); |
| | | 1148 | | |
| | 5 | 1149 | | tIlGen.Emit(OpCodes.Pop); |
| | 5 | 1150 | | tIlGen.Emit(OpCodes.Ret); |
| | 5 | 1151 | | } |
| | | 1152 | | |
| | 5 | 1153 | | var tRemoveCallSiteField = tEmitInfo.CallSiteType.GetFieldEvenIfGeneric(tEmitInfo.CallSiteRemoveName); |
| | 5 | 1154 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tRemoveCallSiteField))) |
| | | 1155 | | { |
| | 5 | 1156 | | tIlGen.EmitDynamicMethodInvokeBinder( |
| | 5 | 1157 | | CSharpBinderFlags.InvokeSpecialName | CSharpBinderFlags.ResultDiscarded, |
| | 5 | 1158 | | tEmitInfo.Alias == null ? tRemoveMethod.Name : $"remove_{tEmitInfo.Alias}", |
| | 5 | 1159 | | Enumerable.Empty<Type>(), |
| | 5 | 1160 | | tEmitInfo.ContextType, |
| | 5 | 1161 | | tRemoveMethod.GetParameters(), |
| | 5 | 1162 | | Enumerable.Repeat(default(string), |
| | 5 | 1163 | | tEmitInfo.ResolvedRemoveParamTypes.Length)); |
| | 5 | 1164 | | tIlGen.EmitCallsiteCreate(tEmitInfo.CallSiteRemoveFuncType); |
| | 5 | 1165 | | tIlGen.Emit(OpCodes.Stsfld, tRemoveCallSiteField); |
| | 5 | 1166 | | } |
| | 5 | 1167 | | tIlGen.Emit(OpCodes.Ldsfld, tRemoveCallSiteField); |
| | 5 | 1168 | | tIlGen.Emit(OpCodes.Ldfld, tRemoveCallSiteField.FieldType.GetFieldEvenIfGeneric("Target")); |
| | 5 | 1169 | | tIlGen.Emit(OpCodes.Ldsfld, tRemoveCallSiteField); |
| | 5 | 1170 | | tIlGen.Emit(OpCodes.Ldarg_0); |
| | 5 | 1171 | | tIlGen.Emit(OpCodes.Callvirt, typeof(IActLikeProxy).GetProperty(nameof(IActLikeProxy.Original)).GetGetMethod |
| | 5 | 1172 | | tIlGen.Emit(OpCodes.Ldarg_1); |
| | 5 | 1173 | | tIlGen.EmitCallInvokeFunc(tEmitInfo.CallSiteRemoveFuncType); |
| | 5 | 1174 | | tIlGen.Emit(OpCodes.Pop); |
| | 5 | 1175 | | tIlGen.Emit(OpCodes.Ret); |
| | | 1176 | | |
| | 5 | 1177 | | tMp.SetRemoveOnMethod(tRemoveBuilder); |
| | 5 | 1178 | | } |
| | | 1179 | | |
| | | 1180 | | private void EmitAddEvent(EventBuilder tMp, MethodBuilder tAddBuilder, EventInfo info, MethodInfo tAddMethod, |
| | | 1181 | | FieldInfo tGetField, FieldInfo tSetField, FieldInfo tIsEventField, EmitEventInfo tEmitInfo) |
| | | 1182 | | { |
| | 5 | 1183 | | var tIlGen = tAddBuilder.GetILGenerator(); |
| | | 1184 | | |
| | | 1185 | | |
| | | 1186 | | |
| | 5 | 1187 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tIsEventField))) |
| | | 1188 | | { |
| | 5 | 1189 | | tIlGen.EmitDynamicIsEventBinder(CSharpBinderFlags.None, tEmitInfo.Alias ?? tEmitInfo.Name, tEmitInfo.Con |
| | 5 | 1190 | | tIlGen.EmitCallsiteCreate(tEmitInfo.CallSiteIsEventFuncType); |
| | 5 | 1191 | | tIlGen.Emit(OpCodes.Stsfld, tIsEventField); |
| | 5 | 1192 | | } |
| | | 1193 | | |
| | | 1194 | | |
| | | 1195 | | |
| | 5 | 1196 | | using (tIlGen.EmitBranchTrue( |
| | 5 | 1197 | | load => load.EmitInvocation( |
| | 5 | 1198 | | target => target.EmitInvocation( |
| | 5 | 1199 | | t => t.Emit(OpCodes.Ldsfld, tIsEventField), |
| | 5 | 1200 | | i => i.Emit(OpCodes.Ldfld, tIsEventField.FieldType.GetFieldEvenIfGeneric("Target")) |
| | 5 | 1201 | | ), |
| | 5 | 1202 | | invoke => invoke.EmitCallInvokeFunc(tEmitInfo.CallSiteIsEventFuncType), |
| | 5 | 1203 | | param => param.Emit(OpCodes.Ldsfld, tIsEventField), |
| | 5 | 1204 | | param => param.EmitInvocation( |
| | 5 | 1205 | | t => t.Emit(OpCodes.Ldarg_0), |
| | 5 | 1206 | | i => i.Emit(OpCodes.Callvirt, typeof(IActLikeProxy).GetProperty(nameof(IActLikeProxy.Orig |
| | 5 | 1207 | | ) |
| | 5 | 1208 | | ) |
| | 5 | 1209 | | ) |
| | 5 | 1210 | | ) //if IsEvent Not True |
| | | 1211 | | { |
| | 5 | 1212 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tSetField))) |
| | | 1213 | | { |
| | 5 | 1214 | | tIlGen.EmitDynamicSetBinderDynamicParams(CSharpBinderFlags.ValueFromCompoundAssignment, tEmitInfo.Al |
| | 5 | 1215 | | tEmitInfo.ContextType, typeof(Object)); |
| | 5 | 1216 | | tIlGen.EmitCallsiteCreate(tEmitInfo.CallSiteInvokeSetFuncType); |
| | 5 | 1217 | | tIlGen.Emit(OpCodes.Stsfld, tSetField); |
| | 5 | 1218 | | } |
| | | 1219 | | |
| | 5 | 1220 | | var tAddAssigneField = tEmitInfo.CallSiteType.GetFieldEvenIfGeneric(tEmitInfo.CallSiteAddAssignName); |
| | | 1221 | | |
| | 5 | 1222 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tAddAssigneField))) |
| | | 1223 | | { |
| | 5 | 1224 | | tIlGen.EmitDynamicBinaryOpBinder(CSharpBinderFlags.None, ExpressionType.AddAssign, tEmitInfo.Context |
| | 5 | 1225 | | tEmitInfo.ResolveReturnType); |
| | 5 | 1226 | | tIlGen.EmitCallsiteCreate(tEmitInfo.CallSiteAddAssignFuncType); |
| | 5 | 1227 | | tIlGen.Emit(OpCodes.Stsfld, tAddAssigneField); |
| | 5 | 1228 | | } |
| | | 1229 | | |
| | 5 | 1230 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tGetField))) |
| | | 1231 | | { |
| | 5 | 1232 | | tIlGen.EmitDynamicGetBinder(CSharpBinderFlags.None, tEmitInfo.Alias ?? tEmitInfo.Name, tEmitInfo.Con |
| | 5 | 1233 | | tIlGen.EmitCallsiteCreate(tEmitInfo.CallSiteInvokeGetFuncType); |
| | 5 | 1234 | | tIlGen.Emit(OpCodes.Stsfld, tGetField); |
| | 5 | 1235 | | } |
| | | 1236 | | |
| | | 1237 | | |
| | 5 | 1238 | | tIlGen.Emit(OpCodes.Ldsfld, tSetField); |
| | 5 | 1239 | | tIlGen.Emit(OpCodes.Ldfld, tSetField.FieldType.GetFieldEvenIfGeneric("Target")); |
| | 5 | 1240 | | tIlGen.Emit(OpCodes.Ldsfld, tSetField); |
| | 5 | 1241 | | tIlGen.Emit(OpCodes.Ldarg_0); |
| | 5 | 1242 | | tIlGen.Emit(OpCodes.Callvirt, typeof(IActLikeProxy).GetProperty(nameof(IActLikeProxy.Original)).GetGetMe |
| | | 1243 | | |
| | 5 | 1244 | | tIlGen.Emit(OpCodes.Ldsfld, tAddAssigneField); |
| | 5 | 1245 | | tIlGen.Emit(OpCodes.Ldfld, tAddAssigneField.FieldType.GetFieldEvenIfGeneric("Target")); |
| | 5 | 1246 | | tIlGen.Emit(OpCodes.Ldsfld, tAddAssigneField); |
| | | 1247 | | |
| | 5 | 1248 | | tIlGen.Emit(OpCodes.Ldsfld, tGetField); |
| | 5 | 1249 | | tIlGen.Emit(OpCodes.Ldfld, tGetField.FieldType.GetFieldEvenIfGeneric("Target")); |
| | 5 | 1250 | | tIlGen.Emit(OpCodes.Ldsfld, tGetField); |
| | 5 | 1251 | | tIlGen.Emit(OpCodes.Ldarg_0); |
| | 5 | 1252 | | tIlGen.Emit(OpCodes.Callvirt, typeof(IActLikeProxy).GetProperty(nameof(IActLikeProxy.Original)).GetGetMe |
| | | 1253 | | |
| | 5 | 1254 | | tIlGen.EmitCallInvokeFunc(tEmitInfo.CallSiteInvokeGetFuncType); |
| | | 1255 | | |
| | 5 | 1256 | | tIlGen.Emit(OpCodes.Ldarg_1); |
| | 5 | 1257 | | tIlGen.EmitCallInvokeFunc(tEmitInfo.CallSiteAddAssignFuncType); |
| | | 1258 | | |
| | 5 | 1259 | | tIlGen.EmitCallInvokeFunc(tEmitInfo.CallSiteInvokeSetFuncType); |
| | 5 | 1260 | | tIlGen.Emit(OpCodes.Pop); |
| | 5 | 1261 | | tIlGen.Emit(OpCodes.Ret); |
| | 5 | 1262 | | } |
| | | 1263 | | |
| | 5 | 1264 | | var tAddCallSiteField = tEmitInfo.CallSiteType.GetFieldEvenIfGeneric(tEmitInfo.CallSiteAddName); |
| | | 1265 | | |
| | 5 | 1266 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tAddCallSiteField))) |
| | | 1267 | | { |
| | 5 | 1268 | | tIlGen.EmitDynamicMethodInvokeBinder( |
| | 5 | 1269 | | CSharpBinderFlags.InvokeSpecialName | CSharpBinderFlags.ResultDiscarded, |
| | 5 | 1270 | | tEmitInfo.Alias == null ? tAddMethod.Name : "add_" + tEmitInfo.Alias, |
| | 5 | 1271 | | Enumerable.Empty<Type>(), |
| | 5 | 1272 | | tEmitInfo.ContextType, |
| | 5 | 1273 | | tAddMethod.GetParameters(), |
| | 5 | 1274 | | Enumerable.Repeat(default(string), |
| | 5 | 1275 | | tEmitInfo.ResolvedAddParamTypes.Length)); |
| | 5 | 1276 | | tIlGen.EmitCallsiteCreate(tEmitInfo.CallSiteAddFuncType); |
| | 5 | 1277 | | tIlGen.Emit(OpCodes.Stsfld, tAddCallSiteField); |
| | 5 | 1278 | | } |
| | 5 | 1279 | | tIlGen.Emit(OpCodes.Ldsfld, tAddCallSiteField); |
| | 5 | 1280 | | tIlGen.Emit(OpCodes.Ldfld, tAddCallSiteField.FieldType.GetFieldEvenIfGeneric("Target")); |
| | 5 | 1281 | | tIlGen.Emit(OpCodes.Ldsfld, tAddCallSiteField); |
| | 5 | 1282 | | tIlGen.Emit(OpCodes.Ldarg_0); |
| | 5 | 1283 | | tIlGen.Emit(OpCodes.Callvirt, typeof(IActLikeProxy).GetProperty(nameof(IActLikeProxy.Original)).GetGetMethod |
| | 20 | 1284 | | for (var i = 1; i <= tEmitInfo.ResolvedAddParamTypes.Length; i++) |
| | | 1285 | | { |
| | 5 | 1286 | | tIlGen.EmitLoadArgument(i); |
| | | 1287 | | } |
| | 5 | 1288 | | tIlGen.EmitCallInvokeFunc(tEmitInfo.CallSiteAddFuncType); |
| | 5 | 1289 | | tIlGen.Emit(OpCodes.Pop); |
| | 5 | 1290 | | tIlGen.Emit(OpCodes.Ret); |
| | | 1291 | | |
| | 5 | 1292 | | tMp.SetAddOnMethod(tAddBuilder); |
| | | 1293 | | |
| | 5 | 1294 | | } |
| | | 1295 | | |
| | | 1296 | | |
| | | 1297 | | /// <summary> |
| | | 1298 | | /// Makes the property helper. |
| | | 1299 | | /// </summary> |
| | | 1300 | | /// <param name="builder">The builder.</param> |
| | | 1301 | | /// <param name="typeBuilder">The type builder.</param> |
| | | 1302 | | /// <param name="info">The info.</param> |
| | | 1303 | | /// <param name="emitInfo">The emit info.</param> |
| | | 1304 | | private void MakePropertyHelper(ModuleBuilder builder, TypeBuilder typeBuilder, PropertyEmitInfo emitInfo, Prope |
| | | 1305 | | { |
| | | 1306 | | |
| | | 1307 | | |
| | 98 | 1308 | | emitInfo.ResolvedIndexParamTypes = new Type[] { }; |
| | 98 | 1309 | | if (info != null) |
| | | 1310 | | { |
| | 96 | 1311 | | emitInfo.ResolvedIndexParamTypes = Enumerable.Select(info.GetIndexParameters(), it => it.ParameterType). |
| | | 1312 | | } |
| | | 1313 | | |
| | | 1314 | | |
| | 98 | 1315 | | var tCallSiteInvokeName = emitInfo.CallSiteName; |
| | 98 | 1316 | | var tCStp = DefineBuilderForCallSite(builder, tCallSiteInvokeName); |
| | | 1317 | | |
| | | 1318 | | |
| | 98 | 1319 | | var tConvertGet = emitInfo.CallSiteConvertName; |
| | | 1320 | | |
| | 98 | 1321 | | emitInfo.CallSiteConvertFuncType = DefineCallsiteField(tCStp, this, tConvertGet, emitInfo.ResolveReturnType) |
| | | 1322 | | |
| | 98 | 1323 | | var tInvokeGet = emitInfo.CallSiteInvokeGetName; |
| | 98 | 1324 | | if (info == null || info.CanRead) |
| | | 1325 | | { |
| | 92 | 1326 | | emitInfo.CallSiteInvokeGetFuncType = DefineCallsiteField(tCStp, this, tInvokeGet, typeof(object), emitIn |
| | | 1327 | | } |
| | | 1328 | | |
| | 98 | 1329 | | var tInvokeSet = emitInfo.CallSiteInvokeSetName; |
| | 98 | 1330 | | if (info != null && info.CanWrite) |
| | | 1331 | | { |
| | 30 | 1332 | | emitInfo.ResolvedParamTypes = info.GetSetMethod().GetParameters().Select(it => it.ParameterType).ToArray |
| | | 1333 | | |
| | 30 | 1334 | | emitInfo.CallSiteInvokeSetFuncType = DefineCallsiteField(tCStp, this, tInvokeSet, typeof(object), emitIn |
| | | 1335 | | } |
| | | 1336 | | |
| | 98 | 1337 | | emitInfo.CallSiteType = tCStp.CreateTypeInfo(); |
| | | 1338 | | |
| | | 1339 | | |
| | | 1340 | | |
| | 98 | 1341 | | var tPublicPrivate = MethodAttributes.Public; |
| | 98 | 1342 | | var tPrefixedGet = emitInfo.GetName; |
| | 98 | 1343 | | var tPrefixedSet = emitInfo.SetName; |
| | 98 | 1344 | | var tPrefixedName = emitInfo.Name; |
| | 98 | 1345 | | if (!emitInfo.DefaultInterfaceImplementation) |
| | | 1346 | | { |
| | 3 | 1347 | | tPublicPrivate = MethodAttributes.Private; |
| | 3 | 1348 | | tPrefixedGet = $"{info.DeclaringType.FullName}.{tPrefixedGet}"; |
| | 3 | 1349 | | tPrefixedSet = $"{info.DeclaringType.FullName}.{tPrefixedSet}"; |
| | 3 | 1350 | | tPrefixedName = $"{info.DeclaringType.FullName}.{tPrefixedName}"; |
| | | 1351 | | } |
| | | 1352 | | |
| | | 1353 | | |
| | 98 | 1354 | | var tMp = typeBuilder.DefineProperty(tPrefixedName, PropertyAttributes.None, |
| | 98 | 1355 | | |
| | 98 | 1356 | | CallingConventions.HasThis, |
| | 98 | 1357 | | |
| | 98 | 1358 | | emitInfo.ResolveReturnType, emitInfo.ResolvedIndexParamTypes); |
| | | 1359 | | |
| | | 1360 | | |
| | 98 | 1361 | | MethodBuilder tGetMethodBuilder = null; |
| | 98 | 1362 | | if (info == null || info.CanRead) // q: some props just don't have GET |
| | | 1363 | | { |
| | | 1364 | | //GetMethod |
| | 92 | 1365 | | tGetMethodBuilder = typeBuilder.DefineMethod(tPrefixedGet, |
| | 92 | 1366 | | tPublicPrivate |
| | 92 | 1367 | | | MethodAttributes.SpecialName |
| | 92 | 1368 | | | MethodAttributes.HideBySig |
| | 92 | 1369 | | | MethodAttributes.Virtual |
| | 92 | 1370 | | | MethodAttributes.Final |
| | 92 | 1371 | | | MethodAttributes.NewSlot, |
| | 92 | 1372 | | emitInfo.ResolveReturnType, |
| | 92 | 1373 | | emitInfo.ResolvedIndexParamTypes); |
| | | 1374 | | |
| | | 1375 | | |
| | 92 | 1376 | | if (!emitInfo.DefaultInterfaceImplementation) |
| | | 1377 | | { |
| | 3 | 1378 | | typeBuilder.DefineMethodOverride(tGetMethodBuilder, info.GetGetMethod()); |
| | | 1379 | | } |
| | | 1380 | | |
| | | 1381 | | |
| | 92 | 1382 | | if (info != null && info.GetGetMethod() != null) |
| | | 1383 | | { |
| | 188 | 1384 | | foreach (var tParam in info.GetGetMethod().GetParameters()) |
| | | 1385 | | { |
| | 4 | 1386 | | tGetMethodBuilder.DefineParameter(tParam.Position + 1, AttributesForParam(tParam), tParam.Name); |
| | | 1387 | | } |
| | | 1388 | | } |
| | | 1389 | | } |
| | | 1390 | | |
| | 98 | 1391 | | MethodBuilder tSetMethodBuilder = null; |
| | 98 | 1392 | | if (info != null && info.CanWrite) // q: some props just don't have SET |
| | | 1393 | | { |
| | | 1394 | | //SetMethod |
| | 30 | 1395 | | tSetMethodBuilder = typeBuilder.DefineMethod(tPrefixedSet, |
| | 30 | 1396 | | tPublicPrivate |
| | 30 | 1397 | | | MethodAttributes.SpecialName |
| | 30 | 1398 | | | MethodAttributes.HideBySig |
| | 30 | 1399 | | | MethodAttributes.Virtual |
| | 30 | 1400 | | | MethodAttributes.Final |
| | 30 | 1401 | | | MethodAttributes.NewSlot, |
| | 30 | 1402 | | null, |
| | 30 | 1403 | | emitInfo.ResolvedParamTypes); |
| | | 1404 | | |
| | 30 | 1405 | | if (!emitInfo.DefaultInterfaceImplementation) |
| | | 1406 | | { |
| | 1 | 1407 | | typeBuilder.DefineMethodOverride(tSetMethodBuilder, info.GetSetMethod()); |
| | | 1408 | | } |
| | | 1409 | | |
| | 128 | 1410 | | foreach (var tParam in info.GetSetMethod().GetParameters()) |
| | | 1411 | | { |
| | 34 | 1412 | | tSetMethodBuilder.DefineParameter(tParam.Position + 1, AttributesForParam(tParam), tParam.Name); |
| | | 1413 | | } |
| | | 1414 | | } |
| | | 1415 | | |
| | 98 | 1416 | | EmitProperty( |
| | 98 | 1417 | | typeBuilder, |
| | 98 | 1418 | | tGetMethodBuilder, |
| | 98 | 1419 | | tMp, |
| | 98 | 1420 | | info, |
| | 98 | 1421 | | tSetMethodBuilder, |
| | 98 | 1422 | | emitInfo); |
| | 98 | 1423 | | } |
| | | 1424 | | private abstract class EmitInfo |
| | | 1425 | | { |
| | | 1426 | | public string Name { get; set; } |
| | | 1427 | | public string Alias { get; set; } |
| | | 1428 | | |
| | 690 | 1429 | | protected EmitInfo() |
| | | 1430 | | { |
| | 690 | 1431 | | _callSiteName = |
| | 690 | 1432 | | new Lazy<string>( |
| | 690 | 1433 | | () => $"Impromptu_Callsite_{Name}_{Guid.NewGuid():N}"); |
| | 690 | 1434 | | } |
| | | 1435 | | |
| | | 1436 | | private readonly Lazy<string> _callSiteName; |
| | | 1437 | | public string CallSiteName |
| | | 1438 | | { |
| | 690 | 1439 | | get { return _callSiteName.Value; } |
| | | 1440 | | } |
| | | 1441 | | public bool NonRecursive { get; set; } |
| | | 1442 | | public bool DefaultInterfaceImplementation { get; set; } |
| | | 1443 | | public IEnumerable<string> ArgNames { get; set; } |
| | | 1444 | | public Type[] ResolvedParamTypes { get; set; } |
| | | 1445 | | public Type ResolveReturnType { get; set; } |
| | | 1446 | | public Type CallSiteType { get; set; } |
| | | 1447 | | public Type ContextType { get; set; } |
| | | 1448 | | |
| | | 1449 | | } |
| | | 1450 | | |
| | | 1451 | | private class MethodEmitInfo : EmitInfo |
| | | 1452 | | { |
| | 587 | 1453 | | public MethodEmitInfo() |
| | | 1454 | | { |
| | 587 | 1455 | | CallSiteInvokeName = "Invoke_Method"; |
| | 587 | 1456 | | CallSiteConvertName = "Convert_Method"; |
| | 587 | 1457 | | } |
| | | 1458 | | |
| | | 1459 | | |
| | | 1460 | | |
| | | 1461 | | public string CallSiteInvokeName { get; protected set; } |
| | | 1462 | | public string CallSiteConvertName { get; protected set; } |
| | | 1463 | | |
| | | 1464 | | public Type CallSiteConvertFuncType { get; set; } |
| | | 1465 | | public Type CallSiteInvokeFuncType { get; set; } |
| | | 1466 | | } |
| | | 1467 | | |
| | | 1468 | | public static CustomAttributeBuilder ToCustomAttributeBuilder(CustomAttributeData customAttributeData) |
| | | 1469 | | { |
| | 8 | 1470 | | return new CustomAttributeBuilder( |
| | 8 | 1471 | | customAttributeData.Constructor, |
| | 8 | 1472 | | customAttributeData.ConstructorArguments |
| | 8 | 1473 | | .Select( |
| | 8 | 1474 | | arg => |
| | 8 | 1475 | | arg.Value?.GetType() |
| | 8 | 1476 | | == typeof(ReadOnlyCollection<CustomAttributeTypedArgument>) |
| | 8 | 1477 | | ? ((ReadOnlyCollection<CustomAttributeTypedArgument>)arg.Value) |
| | 8 | 1478 | | .Select(arrayArg => arrayArg.Value) |
| | 8 | 1479 | | .ToArray() |
| | 8 | 1480 | | : arg.Value |
| | 8 | 1481 | | ) |
| | 8 | 1482 | | .ToArray(), |
| | 8 | 1483 | | customAttributeData.NamedArguments |
| | 8 | 1484 | | .Where(arg => arg.MemberInfo.MemberType == MemberTypes.Property) |
| | 8 | 1485 | | .Select(arg => arg.MemberInfo) |
| | 8 | 1486 | | .Cast<PropertyInfo>() |
| | 8 | 1487 | | .ToArray(), |
| | 8 | 1488 | | customAttributeData.NamedArguments |
| | 8 | 1489 | | .Where(arg => arg.MemberInfo.MemberType == MemberTypes.Property) |
| | 8 | 1490 | | .Select(arg => arg.TypedValue.Value) |
| | 8 | 1491 | | .ToArray(), |
| | 8 | 1492 | | customAttributeData.NamedArguments |
| | 8 | 1493 | | .Where(arg => arg.MemberInfo.MemberType == MemberTypes.Field) |
| | 8 | 1494 | | .Select(arg => arg.MemberInfo) |
| | 8 | 1495 | | .Cast<FieldInfo>() |
| | 8 | 1496 | | .ToArray(), |
| | 8 | 1497 | | customAttributeData.NamedArguments |
| | 8 | 1498 | | .Where(arg => arg.MemberInfo.MemberType == MemberTypes.Field) |
| | 8 | 1499 | | .Select(arg => arg.TypedValue.Value) |
| | 8 | 1500 | | .ToArray() |
| | 8 | 1501 | | ); |
| | | 1502 | | } |
| | | 1503 | | |
| | | 1504 | | |
| | | 1505 | | private void EmitProperty( |
| | | 1506 | | TypeBuilder typeBuilder, |
| | | 1507 | | MethodBuilder getMethodBuilder, |
| | | 1508 | | PropertyBuilder tMp, |
| | | 1509 | | PropertyInfo info, |
| | | 1510 | | MethodBuilder setMethodBuilder, |
| | | 1511 | | PropertyEmitInfo emitInfo |
| | | 1512 | | ) |
| | | 1513 | | { |
| | | 1514 | | |
| | 98 | 1515 | | if (emitInfo.ResolvedIndexParamTypes == null) throw new ArgumentNullException(nameof(emitInfo), "ResolvedInd |
| | | 1516 | | |
| | 98 | 1517 | | if (getMethodBuilder != null) |
| | | 1518 | | { |
| | 92 | 1519 | | var tIlGen = getMethodBuilder.GetILGenerator(); |
| | 92 | 1520 | | var tConvertCallsiteField = emitInfo.CallSiteType.GetFieldEvenIfGeneric(emitInfo.CallSiteConvertName); |
| | | 1521 | | |
| | | 1522 | | //If it's an interface and not nonrecursive this will be true |
| | 92 | 1523 | | var tRecurse = emitInfo.ResolveReturnType.IsInterface && !emitInfo.NonRecursive; |
| | | 1524 | | |
| | | 1525 | | //If we want to do recursive Interfaces we need to test the interface before we dynamically cast |
| | 92 | 1526 | | var tReturnLocal = tIlGen.DeclareLocal(tRecurse ? typeof(object) : emitInfo.ResolveReturnType); |
| | | 1527 | | |
| | | 1528 | | |
| | 92 | 1529 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tConvertCallsiteField))) |
| | | 1530 | | { |
| | 92 | 1531 | | tIlGen.EmitDynamicConvertBinder(CSharpBinderFlags.None, emitInfo.ResolveReturnType, emitInfo.Context |
| | 92 | 1532 | | tIlGen.EmitCallsiteCreate(emitInfo.CallSiteConvertFuncType); |
| | 92 | 1533 | | tIlGen.Emit(OpCodes.Stsfld, tConvertCallsiteField); |
| | 92 | 1534 | | } |
| | | 1535 | | |
| | 92 | 1536 | | var tInvokeGetCallsiteField = emitInfo.CallSiteType.GetFieldEvenIfGeneric(emitInfo.CallSiteInvokeGetName |
| | | 1537 | | |
| | 92 | 1538 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tInvokeGetCallsiteField))) |
| | | 1539 | | { |
| | 92 | 1540 | | tIlGen.EmitDynamicGetBinder(CSharpBinderFlags.None, emitInfo.Alias ?? emitInfo.Name, emitInfo.Contex |
| | 92 | 1541 | | tIlGen.EmitCallsiteCreate(emitInfo.CallSiteInvokeGetFuncType); |
| | 92 | 1542 | | tIlGen.Emit(OpCodes.Stsfld, tInvokeGetCallsiteField); |
| | 92 | 1543 | | } |
| | | 1544 | | |
| | | 1545 | | |
| | | 1546 | | |
| | | 1547 | | |
| | 92 | 1548 | | if (!tRecurse) |
| | | 1549 | | { |
| | 86 | 1550 | | tIlGen.Emit(OpCodes.Ldsfld, tConvertCallsiteField); |
| | 86 | 1551 | | tIlGen.Emit(OpCodes.Ldfld, tConvertCallsiteField.FieldType.GetFieldEvenIfGeneric("Target")); |
| | 86 | 1552 | | tIlGen.Emit(OpCodes.Ldsfld, tConvertCallsiteField); |
| | | 1553 | | } |
| | 92 | 1554 | | tIlGen.Emit(OpCodes.Ldsfld, tInvokeGetCallsiteField); |
| | 92 | 1555 | | tIlGen.Emit(OpCodes.Ldfld, tInvokeGetCallsiteField.FieldType.GetFieldEvenIfGeneric("Target")); |
| | 92 | 1556 | | tIlGen.Emit(OpCodes.Ldsfld, tInvokeGetCallsiteField); |
| | 92 | 1557 | | tIlGen.Emit(OpCodes.Ldarg_0); |
| | 92 | 1558 | | tIlGen.Emit(OpCodes.Callvirt, typeof(IActLikeProxy).GetProperty(nameof(IActLikeProxy.Original)).GetGetMe |
| | 192 | 1559 | | for (var i = 1; i <= emitInfo.ResolvedIndexParamTypes.Length; i++) |
| | | 1560 | | { |
| | 4 | 1561 | | tIlGen.EmitLoadArgument(i); |
| | | 1562 | | } |
| | 92 | 1563 | | tIlGen.EmitCallInvokeFunc(emitInfo.CallSiteInvokeGetFuncType); |
| | | 1564 | | |
| | | 1565 | | //If we are are recursing than do it later |
| | 92 | 1566 | | if (!tRecurse) |
| | | 1567 | | { |
| | 86 | 1568 | | tIlGen.EmitCallInvokeFunc(emitInfo.CallSiteConvertFuncType); |
| | | 1569 | | } |
| | | 1570 | | |
| | 92 | 1571 | | tIlGen.EmitStoreLocation(tReturnLocal.LocalIndex); |
| | | 1572 | | |
| | | 1573 | | |
| | | 1574 | | //If return type is interface and instance is not interface, try actlike |
| | 92 | 1575 | | if (tRecurse) |
| | | 1576 | | { |
| | 6 | 1577 | | using (tIlGen.EmitBranchFalse(gen => gen.EmitLoadLocation(tReturnLocal.LocalIndex))) |
| | | 1578 | | { |
| | 6 | 1579 | | tIlGen.EmitLoadLocation(tReturnLocal.LocalIndex); |
| | 6 | 1580 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Isinst, emitInfo.ResolveReturnType))) |
| | | 1581 | | { |
| | 6 | 1582 | | tIlGen.Emit(OpCodes.Ldarg_0); |
| | 6 | 1583 | | tIlGen.Emit(OpCodes.Callvirt, typeof(IActLikeProxy).GetProperty(nameof(IActLikeProxy.Maker)) |
| | 6 | 1584 | | tIlGen.EmitLoadLocation(tReturnLocal.LocalIndex); |
| | 6 | 1585 | | tIlGen.Emit(OpCodes.Call, ActLikeRec.MakeGenericMethod(emitInfo.ResolveReturnType)); |
| | 6 | 1586 | | tIlGen.Emit(OpCodes.Ret); |
| | 6 | 1587 | | } |
| | | 1588 | | } |
| | | 1589 | | } |
| | | 1590 | | |
| | 92 | 1591 | | var tReturnLabel = tIlGen.DefineLabel(); |
| | 92 | 1592 | | tIlGen.Emit(OpCodes.Br_S, tReturnLabel); |
| | 92 | 1593 | | tIlGen.MarkLabel(tReturnLabel); |
| | 92 | 1594 | | if (tRecurse) |
| | | 1595 | | { |
| | 6 | 1596 | | tIlGen.Emit(OpCodes.Ldsfld, tConvertCallsiteField); |
| | 6 | 1597 | | tIlGen.Emit(OpCodes.Ldfld, tConvertCallsiteField.FieldType.GetFieldEvenIfGeneric("Target")); |
| | 6 | 1598 | | tIlGen.Emit(OpCodes.Ldsfld, tConvertCallsiteField); |
| | | 1599 | | } |
| | 92 | 1600 | | tIlGen.EmitLoadLocation(tReturnLocal.LocalIndex); |
| | 92 | 1601 | | if (tRecurse) |
| | | 1602 | | { |
| | 6 | 1603 | | tIlGen.EmitCallInvokeFunc(emitInfo.CallSiteConvertFuncType); |
| | | 1604 | | } |
| | | 1605 | | |
| | 92 | 1606 | | tIlGen.Emit(OpCodes.Ret); |
| | 92 | 1607 | | tMp.SetGetMethod(getMethodBuilder); |
| | | 1608 | | } |
| | | 1609 | | |
| | 98 | 1610 | | if (setMethodBuilder != null) |
| | | 1611 | | { |
| | 30 | 1612 | | var tIlGen = setMethodBuilder.GetILGenerator(); |
| | 30 | 1613 | | var tSetCallsiteField = emitInfo.CallSiteType.GetFieldEvenIfGeneric(emitInfo.CallSiteInvokeSetName); |
| | | 1614 | | |
| | 30 | 1615 | | using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tSetCallsiteField))) |
| | | 1616 | | { |
| | 30 | 1617 | | tIlGen.EmitDynamicSetBinder(CSharpBinderFlags.None, emitInfo.Alias ?? emitInfo.Name, emitInfo.Contex |
| | 30 | 1618 | | tIlGen.EmitCallsiteCreate(emitInfo.CallSiteInvokeSetFuncType); |
| | 30 | 1619 | | tIlGen.Emit(OpCodes.Stsfld, tSetCallsiteField); |
| | 30 | 1620 | | } |
| | 30 | 1621 | | tIlGen.Emit(OpCodes.Ldsfld, tSetCallsiteField); |
| | 30 | 1622 | | tIlGen.Emit(OpCodes.Ldfld, tSetCallsiteField.FieldType.GetFieldEvenIfGeneric("Target")); |
| | 30 | 1623 | | tIlGen.Emit(OpCodes.Ldsfld, tSetCallsiteField); |
| | 30 | 1624 | | tIlGen.Emit(OpCodes.Ldarg_0); |
| | 30 | 1625 | | tIlGen.Emit(OpCodes.Callvirt, typeof(IActLikeProxy).GetProperty(nameof(IActLikeProxy.Original)).GetGetMe |
| | 128 | 1626 | | for (var i = 1; i <= emitInfo.ResolvedParamTypes.Length; i++) |
| | | 1627 | | { |
| | 34 | 1628 | | tIlGen.EmitLoadArgument(i); |
| | | 1629 | | } |
| | 30 | 1630 | | tIlGen.EmitCallInvokeFunc(emitInfo.CallSiteInvokeSetFuncType); |
| | 30 | 1631 | | tIlGen.Emit(OpCodes.Pop); |
| | 30 | 1632 | | tIlGen.Emit(OpCodes.Ret); |
| | 30 | 1633 | | tMp.SetSetMethod(setMethodBuilder); |
| | | 1634 | | } |
| | | 1635 | | |
| | 98 | 1636 | | if (info != null) |
| | | 1637 | | { |
| | | 1638 | | |
| | 208 | 1639 | | foreach (var attrData in info.GetCustomAttributesData()) |
| | | 1640 | | { |
| | 8 | 1641 | | tMp.SetCustomAttribute(ToCustomAttributeBuilder(attrData)); |
| | | 1642 | | } |
| | | 1643 | | } |
| | 98 | 1644 | | } |
| | | 1645 | | |
| | | 1646 | | |
| | | 1647 | | private Type UpdateCallsiteFuncType(Type tFuncGeneric, Type returnType, params Type[] argTypes) |
| | | 1648 | | { |
| | 318 | 1649 | | var tList = new List<Type> { typeof(CallSite), typeof(object) }; |
| | 318 | 1650 | | tList.AddRange(argTypes); |
| | 318 | 1651 | | if (returnType != typeof(void)) |
| | 318 | 1652 | | tList.Add(returnType); |
| | | 1653 | | |
| | 318 | 1654 | | IEnumerable<Type> tTypeArguments = tList; |
| | | 1655 | | |
| | | 1656 | | |
| | 318 | 1657 | | var tDef = tFuncGeneric.GetGenericTypeDefinition(); |
| | | 1658 | | |
| | 318 | 1659 | | if (tDef.GetGenericArguments().Count() != tTypeArguments.Count()) |
| | | 1660 | | { |
| | 1 | 1661 | | tTypeArguments = tTypeArguments.Where(it => it.IsGenericParameter); |
| | | 1662 | | } |
| | | 1663 | | |
| | 318 | 1664 | | var tFuncType = tDef.MakeGenericType(tTypeArguments.ToArray()); |
| | | 1665 | | |
| | 318 | 1666 | | return tFuncType; |
| | | 1667 | | } |
| | | 1668 | | |
| | | 1669 | | private Type ReduceToElementType(Type type) |
| | | 1670 | | { |
| | 1010 | 1671 | | if (type.IsByRef || type.IsPointer || type.IsArray) |
| | 8 | 1672 | | return type.GetElementType(); |
| | 1002 | 1673 | | return type; |
| | | 1674 | | } |
| | | 1675 | | |
| | | 1676 | | |
| | | 1677 | | |
| | | 1678 | | |
| | | 1679 | | /// <summary> |
| | | 1680 | | /// Emits new delegate type of the call site func. |
| | | 1681 | | /// </summary> |
| | | 1682 | | /// <param name="argTypes">The arg types.</param> |
| | | 1683 | | /// <param name="returnType">Type of the return.</param> |
| | | 1684 | | /// <returns></returns> |
| | | 1685 | | public Type EmitCallSiteFuncType(IEnumerable<Type> argTypes, Type returnType) |
| | | 1686 | | { |
| | 0 | 1687 | | return GenerateCallSiteFuncType(argTypes, returnType); |
| | | 1688 | | } |
| | | 1689 | | |
| | | 1690 | | /// <summary> |
| | | 1691 | | /// Generates the delegate type of the call site function. |
| | | 1692 | | /// </summary> |
| | | 1693 | | /// <param name="argTypes">The arg types.</param> |
| | | 1694 | | /// <param name="returnType">Type of the return.</param> |
| | | 1695 | | /// <param name="methodInfo">The method info. Required for reference types or delegates with more than 16 argume |
| | | 1696 | | /// <param name="builder">The Type Builder. Required for reference types or delegates with more than 16 argument |
| | | 1697 | | /// <returns></returns> |
| | | 1698 | | internal Type GenerateCallSiteFuncType(IEnumerable<Type> argTypes, Type returnType, MethodInfo methodInfo = null |
| | | 1699 | | { |
| | 1411 | 1700 | | bool tIsFunc = returnType != typeof(void); |
| | | 1701 | | |
| | | 1702 | | |
| | 1411 | 1703 | | var tList = new List<Type> { typeof(CallSite), typeof(object) }; |
| | 1411 | 1704 | | tList.AddRange(argTypes.Select(it => (it.IsNotPublic && !it.IsByRef) ? typeof(object) : it)); |
| | | 1705 | | |
| | | 1706 | | |
| | | 1707 | | |
| | 1411 | 1708 | | lock (DelegateCacheLock) |
| | | 1709 | | { |
| | | 1710 | | |
| | | 1711 | | |
| | | 1712 | | TypeHash tHash; |
| | | 1713 | | |
| | 1411 | 1714 | | if ((tList.Any(it => it.IsByRef) || tList.Count > 16) && methodInfo != null) |
| | | 1715 | | { |
| | 5 | 1716 | | tHash = TypeHash.Create(strictOrder: true, moreTypes: methodInfo); |
| | | 1717 | | } |
| | | 1718 | | else |
| | | 1719 | | { |
| | 1406 | 1720 | | tHash = TypeHash.Create(strictOrder: true, moreTypes: tList.Concat(new[] { returnType }).ToArray()); |
| | | 1721 | | } |
| | | 1722 | | |
| | 1411 | 1723 | | if (_delegateCache.TryGetValue(tHash, out var tType)) |
| | | 1724 | | { |
| | 999 | 1725 | | return tType; |
| | | 1726 | | } |
| | | 1727 | | |
| | 412 | 1728 | | if (tList.Any(it => it.IsByRef) |
| | 412 | 1729 | | || (tIsFunc && tList.Count >= 16) |
| | 412 | 1730 | | || (!tIsFunc && tList.Count >= 16)) |
| | | 1731 | | { |
| | 4 | 1732 | | tType = GenerateFullDelegate(builder, returnType, tList, methodInfo); |
| | | 1733 | | |
| | | 1734 | | |
| | 4 | 1735 | | _delegateCache[tHash] = tType; |
| | 4 | 1736 | | return tType; |
| | | 1737 | | } |
| | | 1738 | | |
| | | 1739 | | |
| | | 1740 | | |
| | 408 | 1741 | | if (tIsFunc) |
| | 401 | 1742 | | tList.Add(returnType); |
| | | 1743 | | |
| | 408 | 1744 | | var tFuncGeneric = GenericDelegateType(tList.Count, !tIsFunc); |
| | | 1745 | | |
| | | 1746 | | |
| | 408 | 1747 | | var tFuncType = tFuncGeneric.MakeGenericType(tList.ToArray()); |
| | | 1748 | | |
| | 408 | 1749 | | _delegateCache[tHash] = tFuncType; |
| | | 1750 | | |
| | 408 | 1751 | | return tFuncType; |
| | | 1752 | | |
| | | 1753 | | } |
| | | 1754 | | |
| | | 1755 | | |
| | | 1756 | | |
| | 1411 | 1757 | | } |
| | | 1758 | | |
| | | 1759 | | // ReSharper disable UnusedParameter.Local |
| | | 1760 | | // May switch to nested types if i figure out how to do it, thus would need the typebuilder |
| | | 1761 | | private Type GenerateFullDelegate(TypeBuilder builder, Type returnType, IEnumerable<Type> types, MethodInfo info |
| | | 1762 | | // ReSharper restore UnusedParameter.Local |
| | | 1763 | | { |
| | 4 | 1764 | | var tBuilder = Builder.DefineType( |
| | 4 | 1765 | | $"Impromptu_{"Delegate"}_{Guid.NewGuid():N}", |
| | 4 | 1766 | | TypeAttributes.Class | TypeAttributes.AnsiClass | TypeAttributes.Sealed | TypeAttributes.Public, |
| | 4 | 1767 | | typeof(MulticastDelegate)); |
| | | 1768 | | |
| | 4 | 1769 | | var tReplacedTypes = GetParamTypes(tBuilder, info); |
| | | 1770 | | |
| | 4 | 1771 | | var tParamTypes = info == null |
| | 4 | 1772 | | ? types.ToList() |
| | 4 | 1773 | | : info.GetParameters().Select(it => it.ParameterType).ToList(); |
| | | 1774 | | |
| | 4 | 1775 | | if (tReplacedTypes != null) |
| | | 1776 | | { |
| | 1 | 1777 | | tParamTypes = tReplacedTypes.Item2.ToList(); |
| | | 1778 | | } |
| | | 1779 | | |
| | 4 | 1780 | | if (info != null) |
| | | 1781 | | { |
| | 4 | 1782 | | tParamTypes.Insert(0, typeof(object)); |
| | 4 | 1783 | | tParamTypes.Insert(0, typeof(CallSite)); |
| | | 1784 | | } |
| | | 1785 | | |
| | 4 | 1786 | | var tCon = tBuilder.DefineConstructor( |
| | 4 | 1787 | | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig | |
| | 4 | 1788 | | MethodAttributes.RTSpecialName, CallingConventions.Standard, |
| | 4 | 1789 | | new[] { typeof(object), typeof(IntPtr) }); |
| | | 1790 | | |
| | 4 | 1791 | | tCon.SetImplementationFlags(MethodImplAttributes.CodeTypeMask); |
| | | 1792 | | |
| | 4 | 1793 | | var tMethod = tBuilder.DefineMethod("Invoke", |
| | 4 | 1794 | | MethodAttributes.Public | MethodAttributes.HideBySig | |
| | 4 | 1795 | | MethodAttributes.NewSlot | |
| | 4 | 1796 | | MethodAttributes.Virtual); |
| | | 1797 | | |
| | 4 | 1798 | | tMethod.SetReturnType(returnType); |
| | 4 | 1799 | | tMethod.SetParameters(tParamTypes.ToArray()); |
| | | 1800 | | |
| | 4 | 1801 | | if (info != null) |
| | | 1802 | | { |
| | 16 | 1803 | | foreach (var tParam in info.GetParameters()) |
| | | 1804 | | { |
| | | 1805 | | //+3 because of the callsite and target are added |
| | 4 | 1806 | | tMethod.DefineParameter(tParam.Position + 3, AttributesForParam(tParam), tParam.Name); |
| | | 1807 | | } |
| | | 1808 | | } |
| | | 1809 | | |
| | 4 | 1810 | | tMethod.SetImplementationFlags(MethodImplAttributes.CodeTypeMask); |
| | | 1811 | | |
| | | 1812 | | |
| | | 1813 | | |
| | 4 | 1814 | | return tBuilder.CreateTypeInfo(); |
| | | 1815 | | |
| | | 1816 | | } |
| | | 1817 | | |
| | | 1818 | | private ParameterAttributes AttributesForParam(ParameterInfo param) |
| | | 1819 | | { |
| | 739 | 1820 | | return param.Attributes; |
| | | 1821 | | } |
| | | 1822 | | |
| | | 1823 | | public AssemblyBuilderAccess AssemblyAccess { get; private set; } |
| | | 1824 | | |
| | | 1825 | | |
| | | 1826 | | |
| | | 1827 | | |
| | | 1828 | | private static void GenerateAssembly(string name, AssemblyBuilderAccess access, ref AssemblyBuilder ab, ref Modu |
| | | 1829 | | { |
| | 2 | 1830 | | var tName = new AssemblyName(name); |
| | | 1831 | | |
| | 2 | 1832 | | ab = |
| | 2 | 1833 | | CompatHelper.DefineDynamicAssembly( |
| | 2 | 1834 | | tName, |
| | 2 | 1835 | | access); |
| | | 1836 | | |
| | 2 | 1837 | | mb = |
| | 2 | 1838 | | |
| | 2 | 1839 | | #if NETFRAMEWORK |
| | 2 | 1840 | | access.HasFlag(AssemblyBuilderAccess.Save) |
| | 2 | 1841 | | ? ab.DefineDynamicModule("MainModule", $"{tName.Name}.dll") : |
| | 2 | 1842 | | #endif |
| | 2 | 1843 | | |
| | 2 | 1844 | | ab.DefineDynamicModule("MainModule"); |
| | 2 | 1845 | | } |
| | | 1846 | | } |
| | | 1847 | | } |