private static void CreateClassMembers(IClass t, IGenerator g) { var wrapped = t.Field(_interface, _wrappedFieldName); var ctor = t.Constructor(g.Arg <IReadWriteProperty>("wrapped")).Public; using (var c = ctor.Code()) { c.Assign(wrapped, ctor.Args[0]); } var readWriteProperty = t.Property(typeof(int), _propertyName).Public; var getter = readWriteProperty.Getter(); using (var c = getter.Code()) { c.Return(wrapped.Property(_propertyName)); } var setter = readWriteProperty.Setter(); using (var c = setter.Code()) { c.Assign(wrapped.Property(_propertyName), setter.Value); } }
private IClass DefineProxy() { _interfaceType = typeof(T); ProcessMarkingAttribute(_interfaceType); MapMethods(); // public class ProxyIValueObject : ChangeTrackerBase, IValueObject _proxy = g.Class("Proxy" + _interfaceType.Name).In(_namespace).Inherits(_baseType).Implements(_interfaceType).Public; { // private readonly IValueObject _target; _target = _proxy.Field(_interfaceType, "_target").Internal.ReadOnly; ImplConstructor(); var pi = _baseType.GetProperty( "Target", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (pi != null && pi.PropertyType.IsAssignableFrom(_interfaceType) && pi.GetGetMethod(true).IsAbstract&& pi.GetSetMethod(true) == null) { ImplTargetProperty(pi); } ImplGetTarget(); ImplInterface(); } return(_proxy); }
private static void CreateClassMembers(IClass t, IGenerator g) { var wrapped = t.Field(_interface, _wrappedFieldName); var ctor = t.Constructor(g.Arg <IIndexerProperty>("wrapped")).Public; using (var c = ctor.Code()) { c.Assign(wrapped, ctor.Args[0]); } var indexerProperty = t.Indexer(typeof(long), g.Arg <int>("i"), g.Arg <string>("s")).Public; var getter = indexerProperty.Getter(); using (var c = getter.Code()) { c.Return(wrapped.Indexer(getter.Args[0], getter.Args[1])); } var setter = indexerProperty.Setter(); using (var c = setter.Code()) { c.Assign(wrapped.Indexer(setter.Args[0], setter.Args[1]), setter.Value); } }
[Test] public void InvokeChokesOnNonExistentMethod() { IClass c = _emmiter.Class("Nonexistent").In(_namespace); { var f = c.Field(_interface, _wrappedFieldName); var m = c.Method(typeof(string), _methodName, _emmiter.Arg <int>("i")).Public; using (var code = m.Code()) { var e = Assert.Throws <ArgumentException>(() => f.Invoke("NonExistentMethod", m.Args[0])); System.Diagnostics.Debug.WriteLine(e.Message); } } }
public void InvokeChokesOnNonExistentMethod() { IClass c = _emmiter.Class("Nonexistent").In(_namespace); { var f = c.Field(_interface, _wrappedFieldName); var p = c.Property(typeof(string), _propertyName).Public; var m = p.Getter(); using (m.Code()) { var e = Assert.Throws <ArgumentException>(() => f.Property("NonExistentProperty")); System.Diagnostics.Debug.WriteLine(e.Message); } } }
public void InvokeChokesOnNonExistentMethod() { var g = _emmiter; IClass c = _emmiter.Class("Nonexistent").In(_namespace); { var f = c.Field(_interface, _wrappedFieldName); var p = c.Indexer(typeof(long), g.Arg <int>("i"), g.Arg <string>("s")).Public; var m = p.Getter(); using (m.Code()) { var e = Assert.Throws <ArgumentException>(() => f.Indexer(m.Args[0])); System.Diagnostics.Debug.WriteLine(e.Message); } } }
private void CreateClassMembers(IClass c) { var wrapped = c.Field(_interface, _wrappedFieldName); var ctor = c.Constructor(_g.Arg <IOutParamMethod>("wrapped")).Public; using (var code = ctor.Code()) { code.Assign(wrapped, ctor.Args[0]); } var simpleMethod = c.Method(_methodName, _g.ArgOut <int>("i")).Public; using (var code = simpleMethod.Code()) { var result = wrapped.Invoke(_methodName, simpleMethod.Args[0]); code.Return(result); } }
private void CreateClassMembers(IClass c) { var wrapped = c.Field(_interface, _wrappedFieldName); var ctor = c.Constructor(_emmiter.Arg <IWriteOnlyProperty>("wrapped")).Public; using (var code = ctor.Code()) { code.Assign(wrapped, ctor.Args[0]); } var writeOnlyProperty = c.Property(typeof(int), _propertyName).Public; var m = writeOnlyProperty.Setter(); using (var code = m.Code()) { code.Assign(wrapped.Property(_propertyName), m.Value); } }
private IClass DefineWrapper() { IClass c = _g.Class("HodgepodgeWrapper").In(_namespace).Implements(_interface); { var wrapped = c.Field(_interface, _wrappedFieldName); var ctor = c.Constructor(_g.Arg <IHodgepodge>("wrapped")).Public; using (var c1 = ctor.Code()) { c1.Assign(wrapped, ctor.Args[0]); } foreach (MemberInfo info in _interface.GetMembers(BindingFlags.Public | BindingFlags.Instance)) { switch (info.MemberType) { case MemberTypes.Method: MethodInfo mi = (MethodInfo)info; if (!mi.IsSpecialName) { WrapMethod(c, wrapped, mi); } break; case MemberTypes.Property: PropertyInfo pi = (PropertyInfo)info; WrapProperty(c, wrapped, pi); break; default: Console.WriteLine(info + " : " + info.MemberType); break; } } } return(c); }
private void ImplProxyTargetProperty(PropertyInfo pi, FactoryPair pair) { var propertychangedRaiser = FindOnPropertyChangedMethod(pi); var propertyType = pi.PropertyType; // private IValueComponent _ComponentProperty; var backingField = _proxy.Field(propertyType, "_" + pi.Name); // public IValueComponent ComponentProperty ImplProperty( pi, getter => { // get using (var c = getter.Code()) { // var p = _target.ComponentProperty; var p = c.Variable(propertyType, "p", _target.Property(pi, getter.Args.AsOperands())); // if (p == null) c.If(c.IsNull(p)); { // _ComponentProperty = null; c.Assign(backingField, g.Null(propertyType)); // return null; c.Return(g.Null(propertyType)); } c.End(); // if (_ComponentProperty == null || !ReferenceEquals((NotifyPropertyChangeFactory.GetTarget(_ComponentProperty), p)) c.If(c.Or(c.IsNull(backingField), c.NotReferenceEquals(c.Invoke(pair.GetTarget, backingField), p))); { // _ComponentProperty = NotifyPropertyChangeFactory.GetProxy(p); c.Assign(backingField, c.Invoke(pair.GetProxy, p)); } c.End(); // return _ComponentProperty; c.Return(backingField); } }, setter => { // set using (var c = setter.Code()) { // var newTarget = NotifyPropertyChangeFactory.GetTarget(value); var newTarget = c.Variable(propertyType, "newTarget", c.Invoke(pair.GetTarget, setter.Value)); // if (!ReferenceEquals(_target.ComponentProperty, newTarget)) c.If(c.NotReferenceEquals(_target.Property(pi), newTarget)); { // _target.ComponentProperty = newTarget; c.Assign(_target.Property(pi), newTarget); // _ComponentProperty = NotifyPropertyChangeFactory.GetProxy(value); c.Assign(backingField, c.Invoke(pair.GetProxy, setter.Value)); // FirePropertyChanged("ComponentProperty"); if (propertychangedRaiser != null) { c.Call(_proxy.This.Invoke(propertychangedRaiser, g.Const(pi.Name))); } } c.End(); } }); // end of ComponentProperty }