DeclareLocal() public method

public DeclareLocal ( Type type ) : LocalReference
type System.Type
return Castle.DynamicProxy.Generators.Emitters.SimpleAST.LocalReference
Example #1
0
        protected override void CustomizeGetObjectData(AbstractCodeBuilder codebuilder,
		                                               ArgumentReference arg1, ArgumentReference arg2)
        {
            Type[] key_and_object = new Type[] {typeof(String), typeof(Object)};
            Type[] key_and_bool = new Type[] {typeof(String), typeof(bool)};
            MethodInfo addValueMethod = typeof(SerializationInfo).GetMethod("AddValue", key_and_object);
            MethodInfo addValueBoolMethod = typeof(SerializationInfo).GetMethod("AddValue", key_and_bool);

            codebuilder.AddStatement(new ExpressionStatement(
                                     	new MethodInvocationExpression(arg1, addValueBoolMethod,
                                     	                               new ConstReference("__delegateToBase").ToExpression(),
                                     	                               new ConstReference(delegateToBaseGetObjectData ? 1 : 0).
                                     	                               	ToExpression())));

            if (delegateToBaseGetObjectData)
            {
                MethodInfo baseGetObjectData = targetType.GetMethod("GetObjectData",
                                                                    new Type[] {typeof(SerializationInfo), typeof(StreamingContext)});

                codebuilder.AddStatement(new ExpressionStatement(
                                         	new MethodInvocationExpression(baseGetObjectData,
                                         	                               arg1.ToExpression(), arg2.ToExpression())));
            }
            else
            {
                LocalReference members_ref = codebuilder.DeclareLocal(typeof(MemberInfo[]));
                LocalReference data_ref = codebuilder.DeclareLocal(typeof(object[]));

                MethodInfo getSerMembers = typeof(FormatterServices).GetMethod("GetSerializableMembers",
                                                                               new Type[] {typeof(Type)});
                MethodInfo getObjData = typeof(FormatterServices).GetMethod("GetObjectData",
                                                                            new Type[] {typeof(object), typeof(MemberInfo[])});

                codebuilder.AddStatement(new AssignStatement(members_ref,
                                                             new MethodInvocationExpression(null, getSerMembers,
                                                                                            new TypeTokenExpression(targetType))));

                codebuilder.AddStatement(new AssignStatement(data_ref,
                                                             new MethodInvocationExpression(null, getObjData,
                                                                                            SelfReference.Self.ToExpression(),
                                                                                            members_ref.ToExpression())));

                codebuilder.AddStatement(new ExpressionStatement(
                                         	new MethodInvocationExpression(arg1, addValueMethod,
                                         	                               new ConstReference("__data").ToExpression(),
                                         	                               data_ref.ToExpression())));
            }
        }
		private void EmitCustomGetObjectData(AbstractCodeBuilder codebuilder, ArgumentReference serializationInfo)
		{
			var members = codebuilder.DeclareLocal(typeof(MemberInfo[]));
			var data = codebuilder.DeclareLocal(typeof(object[]));

			var getSerializableMembers = new MethodInvocationExpression(
				null,
				FormatterServicesMethods.GetSerializableMembers,
				new TypeTokenExpression(targetType));
			codebuilder.AddStatement(new AssignStatement(members, getSerializableMembers));

			// Sort to keep order on both serialize and deserialize side the same, c.f DYNPROXY-ISSUE-127
			var callSort = new MethodInvocationExpression(
				null,
				TypeUtilMethods.Sort,
				members.ToExpression());
			codebuilder.AddStatement(new AssignStatement(members, callSort));

			var getObjectData = new MethodInvocationExpression(
				null,
				FormatterServicesMethods.GetObjectData,
				SelfReference.Self.ToExpression(),
				members.ToExpression());
			codebuilder.AddStatement(new AssignStatement(data, getObjectData));

			var addValue = new MethodInvocationExpression(
				serializationInfo,
				SerializationInfoMethods.AddValue_Object,
				new ConstReference("__data").ToExpression(),
				data.ToExpression());
			codebuilder.AddStatement(new ExpressionStatement(addValue));
		}
		protected override void CustomizeGetObjectData(AbstractCodeBuilder codebuilder, ArgumentReference serializationInfo, ArgumentReference streamingContext, ClassEmitter emitter)
		{
			codebuilder.AddStatement(new ExpressionStatement(
			                         	new MethodInvocationExpression(serializationInfo, SerializationInfoMethods.AddValue_Bool,
			                         	                               new ConstReference("__delegateToBase").ToExpression(),
			                         	                               new ConstReference(delegateToBaseGetObjectData ? 1 : 0).
			                         	                               	ToExpression())));

			if (delegateToBaseGetObjectData)
			{
				MethodInfo baseGetObjectData = targetType.GetMethod("GetObjectData",
				                                                    new[] {typeof (SerializationInfo), typeof (StreamingContext)});

				codebuilder.AddStatement(new ExpressionStatement(
				                         	new MethodInvocationExpression(baseGetObjectData,
				                         	                               serializationInfo.ToExpression(),
				                         	                               streamingContext.ToExpression())));
			}
			else
			{
				LocalReference members_ref = codebuilder.DeclareLocal(typeof (MemberInfo[]));
				LocalReference data_ref = codebuilder.DeclareLocal(typeof (object[]));

				codebuilder.AddStatement(new AssignStatement(members_ref,
				                                             new MethodInvocationExpression(null,
				                                                                            FormatterServicesMethods.
				                                                                            	GetSerializableMembers,
				                                                                            new TypeTokenExpression(targetType))));

				codebuilder.AddStatement(new AssignStatement(data_ref,
				                                             new MethodInvocationExpression(null,
				                                                                            FormatterServicesMethods.GetObjectData,
				                                                                            SelfReference.Self.ToExpression(),
				                                                                            members_ref.ToExpression())));

				codebuilder.AddStatement(new ExpressionStatement(
				                         	new MethodInvocationExpression(serializationInfo, SerializationInfoMethods.AddValue_Object,
				                         	                               new ConstReference("__data").ToExpression(),
				                         	                               data_ref.ToExpression())));
			}
		}