public BindableProperty(PropertyDefinition property) { Property = property; PropertyType = property.Module.ImportReference(property.PropertyType); BackingField = property.DeclaringType.Fields.SingleOrDefault(f => f.Name == $"<{property.Name}>k__BackingField"); var attribute = property.GetAttribute(WeaverConstants.BindableAttribute); BindingMode = attribute.GetValue(WeaverConstants.BindingMode, XFBindingMode.OneWay); var typeSystem = property.Module.TypeSystem; ValidateValueMethod = ResolveMethod(attribute.GetValue <string>(WeaverConstants.OnValidateValue), $"OnValidate{property.Name}Value", typeSystem.Boolean, WeaverTypes.BindableObject, typeSystem.Object); PropertyChangedMethod = ResolveMethod(attribute.GetValue <string>(WeaverConstants.OnPropertyChanged), $"On{property.Name}Changed", typeSystem.Void, WeaverTypes.BindableObject, typeSystem.Object, typeSystem.Object); PropertyChangingMethod = ResolveMethod(attribute.GetValue <string>(WeaverConstants.OnPropertyChanging), $"On{property.Name}Changing", typeSystem.Void, WeaverTypes.BindableObject, typeSystem.Object, typeSystem.Object); CoerceValueMethod = ResolveMethod(attribute.GetValue <string>(WeaverConstants.OnCoerceValue), $"OnCoerce{property.Name}Value", typeSystem.Object, WeaverTypes.BindableObject, typeSystem.Object); DefaultValueCreatorMethod = ResolveMethod(attribute.GetValue <string>(WeaverConstants.OnCreateValue), $"OnCreate{property.Name}Value", typeSystem.Object, WeaverTypes.BindableObject); OwningType = attribute.GetValue <TypeReference>(WeaverConstants.OwningType) ?? property.DeclaringType; Initializers = FieldInitializer.Create(BackingField); }
public FieldEnumeration Initialize(FieldInitializer initializer) { foreach (Amendment.Field field in this) { field.InitializerMethod = initializer.Method; } return(this); }
public GameplayGameState(IGameCustomizationHandler Handler, FieldInitializer pFieldInitializer, IAudioHandler pAudio, MenuState MainMenu) { Sounds = pAudio; GameHandler = Handler; MainMenuState = MainMenu; PlayField = new TetrisField(Handler.DefaultTheme, Handler); //PlayField.Settings = Settings; PlayField.OnThemeChangeEvent += PlayField_OnThemeChangeEvent; if (pFieldInitializer != null) { pFieldInitializer.Initialize(PlayField); } PlayField.BlockGroupSet += PlayField_BlockGroupSet; PlayField.SetStandardHotLines(); }
/// <summary> /// Intializes a new instance of the <see cref="GeneratedField{T}"/> class (with custom initializer). /// </summary> /// <param name="engine">The code generation engine.</param> /// <param name="isStatic">true to emit a static field; false to emit a member field.</param> /// <param name="name">Name of the field (null to create an anonymous field).</param> /// <param name="visibility">Visibility of the field.</param> /// <param name="initializer">Field initializer that emits code to intitialize the field.</param> internal GeneratedField(CodeGenEngine engine, bool isStatic, string name, Visibility visibility, FieldInitializer initializer) : base(engine) { // ensure that the specified type is public and all nested types are public, too // => otherwise the dynamically created assembly is not able to access it CodeGenHelpers.CheckTypeIsTotallyPublic(typeof(T)); mVisibility = visibility; mInitializer = initializer; mInitialValueFactory = null; mDefaultValue = default(T); mIsStatic = isStatic; mName = name; if (string.IsNullOrWhiteSpace(mName)) { mName = (isStatic ? "s" : "m") + "X" + Guid.NewGuid().ToString("N"); } }
public void AddStaticField_WithInitializer(Visibility visibility, Type fieldType, object expectedValue, FieldInitializer initializer) { // generate the field name string fieldName = "s" + fieldType.Name; // setup code generation module CallbackCodeGenModule module = new CallbackCodeGenModule(); module.Declare = (m) => { MethodInfo genericMethod = typeof(CodeGenEngine) .GetMethods(BindingFlags.Public | BindingFlags.Instance) .Where(x => x.Name == "AddStaticField" && x.IsGenericMethodDefinition && x.GetGenericArguments().Length == 1) .Where(x => x.GetParameters().Select(y => y.ParameterType).SequenceEqual(new[] { typeof(string), typeof(Visibility), typeof(FieldInitializer) })) .Single(); MethodInfo method = genericMethod.MakeGenericMethod(fieldType); method.Invoke(m.Engine, new object[] { fieldName, visibility, initializer }); }; // create the type ClassDefinition classDefinition = new ClassDefinition("MyClass"); classDefinition.AddModule(module); Type classType = CodeGenEngine.CreateClass(classDefinition); Assert.NotNull(classType); Assert.Equal(typeof(object), classType.BaseType); Assert.Equal("MyClass", classType.Name); // check whether the field was generated correctly var field = classType.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); Assert.NotNull(field); Assert.Equal(fieldType, field.FieldType); Assert.Equal(visibility, field.ToVisibility()); // instantiate the type dynamic obj = Activator.CreateInstance(classType); Assert.NotNull(obj); // check whether fields have a value of the expected type and default type var fieldValue = GetStaticFieldValue(classType, fieldName); Assert.Equal(expectedValue, fieldValue); }
public FieldInitializerInfo(FieldInitializer initializer, Binder binder, EqualsValueClauseSyntax equalsValue) { Initializer = initializer; Binder = binder; EqualsValue = equalsValue; }