public void OnGet(PropertyInterceptionInfo propertyInterceptionInfo, object value)
        {
            if (ComparerUtils.Equals(value, (long)30))
            {
                propertyInterceptionInfo.SetValue(9999);
            }

            if (ComparerUtils.Equals(value, (double)66))
            {
                propertyInterceptionInfo.SetValue(78344.8f);
            }
        }
Example #2
0
 public void OnGet(PropertyInterceptionInfo propertyInterceptionInfo, object value)
 {
     if (value == null)
     {
         propertyInterceptionInfo.SetValue(new TestClass());
     }
 }
 public void OnGet(PropertyInterceptionInfo propertyInterceptionInfo, object value)
 {
     if (value == null)
     {
         propertyInterceptionInfo.SetValue(Activator.CreateInstance(propertyInterceptionInfo.PropertyType));
     }
 }
Example #4
0
        public void OnGet(PropertyInterceptionInfo propertyInterceptionInfo, object value)
        {
            if (Convert.ToInt32(value) == 20)
            {
                propertyInterceptionInfo.SetValue("45");
            }

            if (Convert.ToInt32(value) == 5)
            {
                propertyInterceptionInfo.SetValue(TestEnum.Two);
            }

            if (Convert.ToInt32(value) == 12)
            {
                propertyInterceptionInfo.SetValue(232);
            }
        }
        public bool OnSet(PropertyInterceptionInfo propertyInterceptionInfo, object oldValue, object newValue)
        {
            if (propertyInterceptionInfo.PropertyType.IsArray || propertyInterceptionInfo.PropertyType.AreReferenceAssignable(typeof(List <long>)))
            {
                var passed = new List <long>();
                passed.Add(2);
                passed.Add(232);
                passed.Add(5643);
                passed.Add(52435);
                propertyInterceptionInfo.SetValue(passed);
                return(true);
            }

            if (propertyInterceptionInfo.PropertyType == typeof(long) && Convert.ToInt64(newValue) == 66L)
            {
                propertyInterceptionInfo.SetValue(99L);
                return(true);
            }

            return(false);
        }
        public bool OnSet(PropertyInterceptionInfo propertyInterceptionInfo, object oldValue, object newValue)
        {
            if (propertyInterceptionInfo.Instance is HistoryBase instance)
            {
                if (!instance.HistoryEnabled())
                {
                    return(false);
                }
            }
            var command = new GenericCommand(
                () =>
            {
                propertyInterceptionInfo.SetValue(newValue);
            },
                () =>
            {
                propertyInterceptionInfo.SetValue(oldValue);
            });

            CommandManager.PerformCommand(command);
            return(true);
        }
Example #7
0
        /// <exclude/>
        public void OnGet(PropertyInterceptionInfo propertyInterceptionInfo, object value)
        {
            if (this.defaultValue == null)
            {
                this.defaultValue = value;
            }

            using (var registryKey = RegistryKey.OpenBaseKey(this.registryHive, this.registryView))
            {
                using (var subKey = registryKey.CreateSubKey(this.subKey))
                {
                    var result = ConvertToSystemType(subKey.GetValue(this.name ?? propertyInterceptionInfo.PropertyName,
                                                                     this.defaultValue,
                                                                     RegistryValueOptions.None), propertyInterceptionInfo.PropertyType);

                    // If our property's value is not the same as the value in the registry... Let us update
                    // our property's value
                    propertyInterceptionInfo.SetValue(result);
                }
            }
        }
Example #8
0
        /// <summary>
        /// Invoked if the intercepted property getter has been called
        /// </summary>
        /// <param name="propertyInterceptionInfo">
        /// An object that containes information about the intercepted method
        /// </param>
        /// <param name="value">The current value of the property</param>
        public void OnGet(PropertyInterceptionInfo propertyInterceptionInfo, object value)
        {
            if (value == null)
            {
                lock (this.SyncRoot)
                {
                    if (value == null)
                    {
                        // If the property or field implements IEnumerable then it could be an array
                        // or list or anything else if it does not have a contract, then we look at
                        // its child elements... maybe they have a contract
                        if (string.IsNullOrEmpty(this.contractName))
                        {
                            this.contractName = propertyInterceptionInfo.PropertyType.FullName;
                        }

                        object injectionInstance;

                        if (Factory.HasContract(this.contractName))
                        {
                            if (this.arguments == null || this.arguments.Length == 0)
                            {
                                injectionInstance = Factory.Create(this.contractName);
                            }
                            else
                            {
                                injectionInstance = Factory.Create(this.contractName, this.arguments);
                            }
                        }
                        else if (propertyInterceptionInfo.ChildType != null && propertyInterceptionInfo.ChildType != typeof(object))
                        {
                            injectionInstance = Factory.CreateMany(propertyInterceptionInfo.ChildType);
                        }
                        else if (!propertyInterceptionInfo.PropertyType.GetTypeInfo().IsInterface)
                        {
                            // If the property type is not an interface, then we will try to create
                            // the type with its default constructor
                            if (this.arguments == null || this.arguments.Length == 0)
                            {
                                injectionInstance = this.typeToCreate == null?propertyInterceptionInfo.PropertyType.CreateInstance() : this.typeToCreate.CreateInstance();
                            }
                            else
                            {
                                injectionInstance = this.typeToCreate == null?propertyInterceptionInfo.PropertyType.CreateInstance(this.arguments) : this.typeToCreate.CreateInstance(this.arguments);
                            }
                        }
                        else // If everthing else fails... We will throw an exception
                        {
                            throw new InvalidOperationException($"Unable to inject the contract '{this.contractName}' to the property or field '{propertyInterceptionInfo.PropertyName}' in '{propertyInterceptionInfo.DeclaringType.FullName}'. Please make sure that the implementing type has a Component attribute.");
                        }

                        propertyInterceptionInfo.SetValue(injectionInstance);

                        // Add these to auto dispose if possible
                        var disposableInstance = injectionInstance as IDisposable;

                        if (disposableInstance != null)
                        {
                            var disposableMe = propertyInterceptionInfo.Instance as IDisposableObject;
                            // TODO - Maybe auto implement IDisposable???
                            if (disposableMe != null)
                            {
                                disposableMe.Disposed += (s, e) => disposableInstance?.Dispose();
                            }
                            else
                            {
                                Output.WriteLineError($"'{propertyInterceptionInfo.DeclaringType.FullName}' must implement '{typeof(IDisposableObject).FullName}' because '{injectionInstance}' is disposable.");
                            }
                        }
                    }
                }
            }
        }