public bool Weave(PropertyDefinition property) { var atomAttribute = Helpers.GetCustomAttribute <AtomAttribute>(property); if (atomAttribute == null) { return(false); } property.CustomAttributes.Remove(atomAttribute); if (property.GetMethod == null) { _diagnosticMessages.Add(UserError.MakeWarning("UniMobAtomUselessOnSetOnlyProperties", $"Atom attribute on set-only property `{property.Name}` don't make sense", property.SetMethod, null)); return(false); } FixAutoPropertyBackingField(property); var atomField = CreateAtomField(property); property.DeclaringType.Fields.Add(atomField); new AtomGetterMethodWeaver(this, property, atomField).Weave(); if (property.SetMethod != null) { new AtomSetterMethodWeaver(this, property, atomField).Weave(); } return(true); }
private void Prepare(AssemblyDefinition assembly) { _generateDebugNames = Helpers.GetCustomAttribute <AtomGenerateDebugNamesAttribute>(assembly) != null; #if UNITY_EDITOR || UNIMOB_ATOM_GENERATE_DEBUG_NAMES _generateDebugNames = true; #endif _module = assembly.MainModule; _atomType = _module.ImportReference(typeof(ComputedAtom <>)); _lifetimeScopeInterfaceType = _module.ImportReference(typeof(ILifetimeScope)); var atomTypeDef = _atomType.Resolve(); var atomPullDef = _module.ImportReference(typeof(Func <>)).Resolve(); var atomFactoryDef = _module.ImportReference(typeof(CodeGenAtom)).Resolve(); _atomGetValueMethod = _module.ImportReference(atomTypeDef.FindProperty(ValuePropertyName).GetMethod); _atomCreateMethod = _module.ImportReference(atomFactoryDef.FindMethod(CreateAtomMethodName, 4)); _atomDirectEvalMethod = _module.ImportReference(atomTypeDef.FindMethod(DirectEvaluateMethodName, 0)); _atomCompAndInvalidateMethod = _module.ImportReference(atomTypeDef.FindMethod(CompAndInvalidateMethodName, 1)); _atomPullCtorMethod = _module.ImportReference(atomPullDef.FindMethod(ConstructorName, 2)); }
public bool Weave(PropertyDefinition propDef) { var customAttr = Helpers.GetCustomAttribute <AtomAttribute>(propDef); if (customAttr == null) { return(false); } //propDef.CustomAttributes.Remove(customAttr); var pull = (propDef.GetMethod != null) ? CreatePullMethod(propDef) : null; var push = (propDef.SetMethod != null) ? CreatePushMethod(propDef) : null; var atom = CreateAtomField(propDef); if (pull != null) { propDef.DeclaringType.Methods.Add(pull); } if (push != null) { propDef.DeclaringType.Methods.Add(push); } propDef.DeclaringType.Fields.Add(atom); ReplacePropertyCall(propDef, atom, pull, push); return(true); }
public bool Weave(PropertyDefinition property) { var atomAttribute = Helpers.GetCustomAttribute <AtomAttribute>(property); if (atomAttribute == null) { return(false); } if (!property.DeclaringType.IsClass || property.DeclaringType.IsValueType) { _diagnosticMessages.Add(UserError.AtomAttributeCanBeUsedOnlyOnClassMembers(property)); return(false); } if (!property.DeclaringType.IsInterfaceImplemented(_lifetimeScopeInterfaceType)) { _diagnosticMessages.Add(UserError.AtomAttributeCanBeUsedOnlyOnLifetimeScope(property)); return(false); } if (property.GetMethod == null) { _diagnosticMessages.Add(UserError.CannotUseAtomAttributeOnSetOnlyProperty(property)); return(false); } if (property.GetMethod.IsStatic) { _diagnosticMessages.Add(UserError.CannotUseAtomAttributeOnStaticProperty(property)); return(false); } if (property.GetMethod.IsAbstract) { _diagnosticMessages.Add(UserError.CannotUseAtomAttributeOnAbstractProperty(property)); return(false); } var atomOptions = new AtomOptions { KeepAlive = atomAttribute.GetArgumentValueOrDefault(KeepAliveParameterName, false), }; property.CustomAttributes.Remove(atomAttribute); FixAutoPropertyBackingField(property); var atomField = CreateAtomField(property); property.DeclaringType.Fields.Add(atomField); new AtomGetterMethodWeaver(this, property, atomField, atomOptions).Weave(); if (property.SetMethod != null) { new AtomSetterMethodWeaver(this, property, atomField).Weave(); } return(true); }