Exemplo n.º 1
0
        public void InstancePropertyReference()
        {
            CustomPropertyEmitter propertyEmitter = ClassEmitter.CreateProperty("Property", PropertyKind.Instance, typeof(string));

            propertyEmitter.CreateGetMethod();
            propertyEmitter.CreateSetMethod();
            propertyEmitter.ImplementWithBackingField();

            var methodEmitter = GetMethodEmitter(false, typeof(string), new Type[0]);

            LocalReference    oldValueLocal         = methodEmitter.DeclareLocal(typeof(string));
            PropertyReference propertyWithSelfOwner = new PropertyReference(propertyEmitter.PropertyBuilder);

            Assert.That(propertyWithSelfOwner.Type, Is.EqualTo(typeof(string)));

            methodEmitter.AddStatement(new AssignStatement(oldValueLocal, propertyWithSelfOwner.ToExpression()));
            methodEmitter.AddStatement(new AssignStatement(propertyWithSelfOwner, new ConstReference("New").ToExpression()));
            methodEmitter.AddStatement(new ReturnStatement(oldValueLocal));

            object instance = GetBuiltInstance();

            PrivateInvoke.SetPublicProperty(instance, "Property", "Old");
            Assert.That(InvokeMethod(), Is.EqualTo("Old"));
            Assert.That(PrivateInvoke.GetPublicProperty(instance, "Property"), Is.EqualTo("New"));
        }
Exemplo n.º 2
0
        public void BuildPropertyEmitter(ClassEmitter classEmitter)
        {
            if (emitter != null)
            {
                throw new InvalidOperationException("Emitter is already created. It is illegal to invoke this method twice.");
            }

            emitter = classEmitter.CreateProperty(name, attributes, type, arguments);
            foreach (var attribute in customAttributes)
            {
                emitter.DefineCustomAttribute(attribute);
            }
        }