Пример #1
0
        // <summary>
        // Construct an interception delegate for the specified proxy member.
        // </summary>
        // <param name="member"> EdmMember that specifies the member to be intercepted. </param>
        // <param name="proxyType"> Type of the proxy. </param>
        private static void InterceptMember(EdmMember member, Type proxyType, EntityProxyTypeInfo proxyTypeInfo)
        {
            var property = proxyType.GetTopProperty(member.Name);

            Debug.Assert(
                property != null,
                String.Format(
                    CultureInfo.CurrentCulture, "Expected property {0} to be defined on proxy type {1}", member.Name, proxyType.FullName));

            var interceptorField = proxyType.GetField(
                LazyLoadImplementor.GetInterceptorFieldName(member.Name),
                BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.NonPublic);

            Debug.Assert(
                interceptorField != null,
                String.Format(
                    CultureInfo.CurrentCulture, "Expected interceptor field for property {0} to be defined on proxy type {1}", member.Name,
                    proxyType.FullName));

            var interceptorDelegate = GetInterceptorDelegateMethod.
                                      MakeGenericMethod(proxyType, property.PropertyType).
                                      Invoke(null, new object[] { member, proxyTypeInfo.EntityWrapperDelegate }) as Delegate;

            AssignInterceptionDelegate(interceptorDelegate, interceptorField);
        }
Пример #2
0
 public ProxyTypeBuilder(ClrEntityType ospaceEntityType)
 {
     _ospaceEntityType         = ospaceEntityType;
     _baseImplementor          = new BaseProxyImplementor();
     _ipocoImplementor         = new IPocoImplementor(ospaceEntityType);
     _lazyLoadImplementor      = new LazyLoadImplementor(ospaceEntityType);
     _dataContractImplementor  = new DataContractImplementor(ospaceEntityType);
     _iserializableImplementor = new SerializableImplementor(ospaceEntityType);
 }
Пример #3
0
        private static void InterceptMember(
            EdmMember member,
            Type proxyType,
            EntityProxyTypeInfo proxyTypeInfo)
        {
            PropertyInfo topProperty = proxyType.GetTopProperty(member.Name);
            FieldInfo    field       = proxyType.GetField(LazyLoadImplementor.GetInterceptorFieldName(member.Name), BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.NonPublic);

            EntityProxyFactory.AssignInterceptionDelegate(EntityProxyFactory.GetInterceptorDelegateMethod.MakeGenericMethod(proxyType, topProperty.PropertyType).Invoke((object)null, new object[2]
            {
                (object)member,
                (object)proxyTypeInfo.EntityWrapperDelegate
            }) as Delegate, field);
        }
Пример #4
0
        public bool EmitMember(
            TypeBuilder typeBuilder,
            EdmMember member,
            PropertyBuilder propertyBuilder,
            PropertyInfo baseProperty,
            BaseProxyImplementor baseImplementor)
        {
            if (!this._members.Contains(member))
            {
                return(false);
            }
            MethodInfo       meth             = baseProperty.Getter();
            MethodAttributes methodAttributes = meth.Attributes & MethodAttributes.MemberAccessMask;
            Type             type             = typeof(Func <, ,>).MakeGenericType((Type)typeBuilder, baseProperty.PropertyType, typeof(bool));
            MethodInfo       method           = TypeBuilder.GetMethod(type, typeof(Func <, ,>).GetOnlyDeclaredMethod("Invoke"));
            FieldBuilder     fieldBuilder     = typeBuilder.DefineField(LazyLoadImplementor.GetInterceptorFieldName(baseProperty.Name), type, FieldAttributes.Private | FieldAttributes.Static);
            MethodBuilder    mdBuilder        = typeBuilder.DefineMethod("get_" + baseProperty.Name, methodAttributes | MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.SpecialName, baseProperty.PropertyType, Type.EmptyTypes);
            ILGenerator      ilGenerator      = mdBuilder.GetILGenerator();
            Label            label            = ilGenerator.DefineLabel();

            ilGenerator.DeclareLocal(baseProperty.PropertyType);
            ilGenerator.Emit(OpCodes.Ldarg_0);
            ilGenerator.Emit(OpCodes.Call, meth);
            ilGenerator.Emit(OpCodes.Stloc_0);
            ilGenerator.Emit(OpCodes.Ldarg_0);
            ilGenerator.Emit(OpCodes.Ldfld, (FieldInfo)fieldBuilder);
            ilGenerator.Emit(OpCodes.Ldarg_0);
            ilGenerator.Emit(OpCodes.Ldloc_0);
            ilGenerator.Emit(OpCodes.Callvirt, method);
            ilGenerator.Emit(OpCodes.Brtrue_S, label);
            ilGenerator.Emit(OpCodes.Ldarg_0);
            ilGenerator.Emit(OpCodes.Call, meth);
            ilGenerator.Emit(OpCodes.Ret);
            ilGenerator.MarkLabel(label);
            ilGenerator.Emit(OpCodes.Ldloc_0);
            ilGenerator.Emit(OpCodes.Ret);
            propertyBuilder.SetGetMethod(mdBuilder);
            baseImplementor.AddBasePropertyGetter(baseProperty);
            return(true);
        }
 public ProxyTypeBuilder(ClrEntityType ospaceEntityType)
 {
     _ospaceEntityType = ospaceEntityType;
     _baseImplementor = new BaseProxyImplementor();
     _ipocoImplementor = new IPOCOImplementor(ospaceEntityType);
     _lazyLoadImplementor = new LazyLoadImplementor(ospaceEntityType);
     _dataContractImplementor = new DataContractImplementor(ospaceEntityType);
     _iserializableImplementor = new SerializableImplementor(ospaceEntityType);
 }