internal static object LocatePropertyObject(object obj, string mappedName, out string propertyName, bool inDesigner) { object target = obj; obj.GetType(); propertyName = null; int startIndex = 0; while (startIndex < mappedName.Length) { int index = mappedName.IndexOf('.', startIndex); if (index < 0) { break; } propertyName = mappedName.Substring(startIndex, index - startIndex); startIndex = index + 1; target = FastPropertyAccessor.GetProperty(target, propertyName, inDesigner); if (target == null) { return(null); } } if (startIndex > 0) { propertyName = mappedName.Substring(startIndex); return(target); } propertyName = mappedName; return(target); }
private void ApplyTemplateProperties(Control control) { object[] parameters = new object[1]; foreach (TemplatePropertyEntry entry in base.GetFilteredPropertyEntrySet(this._skinBuilder.TemplatePropertyEntries)) { try { if (FastPropertyAccessor.GetProperty(control, entry.Name, base.InDesigner) == null) { ControlBuilder builder = entry.Builder; builder.SetServiceProvider(base.ServiceProvider); try { object obj3 = builder.BuildObject(true); parameters[0] = obj3; } finally { builder.SetServiceProvider(null); } Util.InvokeMethod(entry.PropertyInfo.GetSetMethod(), control, parameters); } } catch (Exception) { } catch { } } }
internal static IDictionary GetPersonalizablePropertyValues(Control control, PersonalizationScope scope, bool excludeSensitive) { IDictionary dictionary = null; IDictionary personalizablePropertyEntries = GetPersonalizablePropertyEntries(control.GetType()); if (personalizablePropertyEntries.Count != 0) { foreach (DictionaryEntry entry in personalizablePropertyEntries) { string key = (string)entry.Key; PersonalizablePropertyEntry entry2 = (PersonalizablePropertyEntry)entry.Value; if ((!excludeSensitive || !entry2.IsSensitive) && ((scope != PersonalizationScope.User) || (entry2.Scope != PersonalizationScope.Shared))) { if (dictionary == null) { dictionary = new HybridDictionary(personalizablePropertyEntries.Count, false); } object y = FastPropertyAccessor.GetProperty(control, key, control.DesignMode); dictionary[key] = new Pair(entry2.PropertyInfo, y); } } } if (dictionary == null) { dictionary = new HybridDictionary(false); } return(dictionary); }
private void ApplyTemplateProperties(Control control) { object[] parameters = new object[1]; ICollection entries = GetFilteredPropertyEntrySet(_skinBuilder.TemplatePropertyEntries); foreach (TemplatePropertyEntry entry in entries) { try { object originalValue = FastPropertyAccessor.GetProperty(control, entry.Name, InDesigner); if (originalValue == null) { ControlBuilder controlBuilder = ((TemplatePropertyEntry)entry).Builder; controlBuilder.SetServiceProvider(ServiceProvider); try { object objectValue = controlBuilder.BuildObject(true); parameters[0] = objectValue; } finally { controlBuilder.SetServiceProvider(null); } MethodInfo methodInfo = entry.PropertyInfo.GetSetMethod(); Util.InvokeMethod(methodInfo, control, parameters); } } catch (Exception e) { Debug.Fail(e.Message); } #pragma warning disable 1058 catch { } #pragma warning restore 1058 } }
private void ExtractTemplateValuesRecursive(ArrayList subBuilders, OrderedDictionary table, Control container) { foreach (object obj2 in subBuilders) { ControlBuilder builder = obj2 as ControlBuilder; if (builder != null) { ICollection boundPropertyEntries; if (!builder.HasFilteredBoundEntries) { boundPropertyEntries = builder.BoundPropertyEntries; } else { ServiceContainer serviceProvider = new ServiceContainer(); serviceProvider.AddService(typeof(IFilterResolutionService), builder.TemplateControl); try { builder.SetServiceProvider(serviceProvider); boundPropertyEntries = builder.GetFilteredPropertyEntrySet(builder.BoundPropertyEntries); } finally { builder.SetServiceProvider(null); } } string strA = null; bool flag = true; Control o = null; foreach (BoundPropertyEntry entry in boundPropertyEntries) { if (entry.TwoWayBound) { string str2; if (string.Compare(strA, entry.ControlID, StringComparison.Ordinal) != 0) { flag = true; } else { flag = false; } strA = entry.ControlID; if (flag) { o = container.FindControl(entry.ControlID); if ((o == null) || !entry.ControlType.IsInstanceOfType(o)) { continue; } } object target = PropertyMapper.LocatePropertyObject(o, entry.Name, out str2, base.InDesigner); table[entry.FieldName] = FastPropertyAccessor.GetProperty(target, str2, base.InDesigner); } } this.ExtractTemplateValuesRecursive(builder.SubBuilders, table, container); } } }
public void Setter_ThrowsArgumentNull_Instance() { // Arrange & Act & Assert var mine = new MyProps(); var accessor = new FastPropertyAccessor <MyProps>(mine.GetType().GetProperty("StringProp")); ExceptionAssert.ThrowsArgumentNull(() => accessor.SetValue(null, 5), "instance"); }
internal static void SetMappedPropertyValue(object obj, string mappedName, object value, bool inDesigner) { string str; object target = LocatePropertyObject(obj, mappedName, out str, inDesigner); if (target != null) { FastPropertyAccessor.SetProperty(target, str, value, inDesigner); } }
public void GetterWorksForReferenceType() { // Arrange var mine = new MyProps(); var accessor = new FastPropertyAccessor<MyProps>(mine.GetType().GetProperty("StringProp")); mine.StringProp = "*4"; // Assert Assert.Equal("*4", accessor.GetValue(mine)); }
public void GetterWorksForValueType() { // Arrange var mine = new MyProps(); var accessor = new FastPropertyAccessor<MyProps>(mine.GetType().GetProperty("IntProp")); mine.IntProp = 4; // Assert Assert.Equal(4, accessor.GetValue(mine)); }
/* * Walks the object model using the mapped property name to set the * value of an instance's property. */ internal static void SetMappedPropertyValue(object obj, string mappedName, object value, bool inDesigner) { string propertyName; object childObj = LocatePropertyObject(obj, mappedName, out propertyName, inDesigner); if (childObj == null) { return; } FastPropertyAccessor.SetProperty(childObj, propertyName, value, inDesigner); }
public void GetterWorksForReferenceType() { // Arrange var mine = new MyProps(); var accessor = new FastPropertyAccessor <MyProps>(mine.GetType().GetProperty("StringProp")); mine.StringProp = "*4"; // Act & Assert Assert.Equal("*4", accessor.GetValue(mine)); }
public void GetterWorksForValueType() { // Arrange var mine = new MyProps(); var accessor = new FastPropertyAccessor <MyProps>(mine.GetType().GetProperty("IntProp")); mine.IntProp = 4; // Act & Assert Assert.Equal(4, accessor.GetValue(mine)); }
public FastPropertyAccessorTests() { this.item = new PublishedItem { Id = 1234, Name = "Foo", CreateDate = DateTime.MaxValue }; this.accessor = new FastPropertyAccessor(typeof(PublishedItem)); }
protected ORIDPropertyUpdater(PropertyInfo propertyInfo) { _propertyInfo = propertyInfo; if (propertyInfo != null) { _getter = FastPropertyAccessor.BuildTypedGetter <TTarget, T>(propertyInfo); if (_propertyInfo.CanWrite) { _setter = FastPropertyAccessor.BuildTypedSetter <TTarget, T>(propertyInfo); } } }
public void Copy_ThrowsArgumentNull_FromAndTo() { // Arrange var accessor = new FastPropertyAccessor <MyProps>(typeof(MyProps).GetProperty("StringProp")); // Act & Assert ExceptionAssert.ThrowsArgumentNull(() => accessor.Copy(null, null), "from"); // Act & Assert MyProps mine = new MyProps(); ExceptionAssert.ThrowsArgumentNull(() => accessor.Copy(mine, null), "to"); }
private void ApplyComplexProperties(Control control) { ICollection entries = GetFilteredPropertyEntrySet(_skinBuilder.ComplexPropertyEntries); foreach (ComplexPropertyEntry entry in entries) { ControlBuilder builder = entry.Builder; if (builder != null) { string propertyName = entry.Name; if (entry.ReadOnly) { object objectValue = FastPropertyAccessor.GetProperty(control, propertyName, InDesigner); if (objectValue == null) { continue; } entry.Builder.SetServiceProvider(ServiceProvider); try { entry.Builder.InitObject(objectValue); } finally { entry.Builder.SetServiceProvider(null); } } else { object childObj; string actualPropName; object value = entry.Builder.BuildObject(true); // Make the UrlProperty based on theme path for control themes(Must be a string) PropertyDescriptor desc = PropertyMapper.GetMappedPropertyDescriptor(control, PropertyMapper.MapNameToPropertyName(propertyName), out childObj, out actualPropName, InDesigner); if (desc != null) { string str = value as string; if (value != null && desc.Attributes[typeof(UrlPropertyAttribute)] != null && UrlPath.IsRelativeUrl(str)) { value = _themePath + str; } } FastPropertyAccessor.SetProperty(childObj, propertyName, value, InDesigner); } } } }
internal static object LocatePropertyObject(object obj, string mappedName, out string propertyName, bool inDesigner) { object currentObject = obj; Type currentType = obj.GetType(); propertyName = null; int index; int startIndex = 0; // step through the dots of the object model to extract the PropertyInfo // and object on which the property will be set while (startIndex < mappedName.Length) { index = mappedName.IndexOf(OM_CHAR, startIndex); // If we didn't find a separator, we're on the last piece if (index < 0) { break; } // There is a sub property, so get its info and iterate propertyName = mappedName.Substring(startIndex, index - startIndex); startIndex = index + 1; currentObject = FastPropertyAccessor.GetProperty(currentObject, propertyName, inDesigner); if (currentObject == null) { return(null); } } // Avoid a useless call to Substring if possible if (startIndex > 0) { propertyName = mappedName.Substring(startIndex); } else { propertyName = mappedName; } return(currentObject); }
/// <inheritdoc/> public override object Map(IPublishedContent content, object value) { // First try custom properties foreach (string name in this.Aliases) { value = this.CheckConvertType(content.GetPropertyValue(name, this.Recursive)); if (!value.IsNullOrEmptyString()) { if (this.PropertyType.IsInstanceOfType(value)) { break; } } } // Then try class properties if (value.IsNullOrEmptyString() || value == this.DefaultValue) { Type contentType = content.GetType(); string key = contentType.AssemblyQualifiedName; if (key != null) { ContentAccessorCache.TryGetValue(key, out var accessor); if (accessor == null) { accessor = new FastPropertyAccessor(contentType); ContentAccessorCache.TryAdd(key, accessor); } foreach (string name in this.Aliases) { value = this.CheckConvertType(accessor.GetValue(name, content)); if (!value.IsNullOrEmptyString() && !value.Equals(this.DefaultValue)) { break; } } } } return(value ?? this.DefaultValue); }
public void Run(PropertyContext pipelineContext, ProcessorSchema processorContext) { try { if (!pipelineContext.Faulted && GetPropertyInfo(pipelineContext, processorContext, out PropertyInfo sourcePropertyInfo)) { pipelineContext.Context = FastPropertyAccessor.GetValue(sourcePropertyInfo, pipelineContext.MappingContext.Input); } } catch { pipelineContext.Faulted = true; throw; } finally { Next(); } }
public void Run(PropertyContext pipelineContext, ProcessorSchema processorContext) { try { if (!pipelineContext.Faulted) { FastPropertyAccessor.SetValue(pipelineContext.PropertyInfo, pipelineContext.MappingContext.Output, pipelineContext.Context); } } catch { pipelineContext.Faulted = true; throw; } finally { Next(); } }
private static IDictionary SetPersonalizedProperties(Control control, IDictionary personalizableProperties, IDictionary propertyState, System.Web.UI.WebControls.WebParts.PersonalizationScope scope) { if (personalizableProperties.Count == 0) { return(propertyState); } if ((propertyState == null) || (propertyState.Count == 0)) { return(null); } IDictionary dictionary = null; foreach (DictionaryEntry entry in propertyState) { string key = (string)entry.Key; object val = entry.Value; PersonalizablePropertyEntry entry2 = (PersonalizablePropertyEntry)personalizableProperties[key]; bool flag = false; if ((entry2 != null) && ((scope == System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared) || (entry2.Scope == System.Web.UI.WebControls.WebParts.PersonalizationScope.User))) { PropertyInfo propertyInfo = entry2.PropertyInfo; try { FastPropertyAccessor.SetProperty(control, key, val, control.DesignMode); flag = true; } catch { } } if (!flag) { if (dictionary == null) { dictionary = new HybridDictionary(propertyState.Count, false); } dictionary[key] = val; } } return(dictionary); }
private void ApplyComplexProperties(Control control) { foreach (ComplexPropertyEntry entry in base.GetFilteredPropertyEntrySet(this._skinBuilder.ComplexPropertyEntries)) { if (entry.Builder != null) { object obj3; string str2; string name = entry.Name; if (entry.ReadOnly) { object obj2 = FastPropertyAccessor.GetProperty(control, name, base.InDesigner); if (obj2 == null) { continue; } entry.Builder.SetServiceProvider(base.ServiceProvider); try { entry.Builder.InitObject(obj2); continue; } finally { entry.Builder.SetServiceProvider(null); } } object val = entry.Builder.BuildObject(true); PropertyDescriptor descriptor = PropertyMapper.GetMappedPropertyDescriptor(control, PropertyMapper.MapNameToPropertyName(name), out obj3, out str2, base.InDesigner); if (descriptor != null) { string virtualPath = val as string; if (((val != null) && (descriptor.Attributes[typeof(UrlPropertyAttribute)] != null)) && UrlPath.IsRelativeUrl(virtualPath)) { val = this._themePath + virtualPath; } } FastPropertyAccessor.SetProperty(obj3, name, val, base.InDesigner); } } }
/// <devdoc> /// </devdoc> internal static IDictionary GetPersonalizablePropertyValues(Control control, PersonalizationScope scope, bool excludeSensitive) { IDictionary propertyBag = null; IDictionary propertyEntries = GetPersonalizablePropertyEntries(control.GetType()); if (propertyEntries.Count != 0) { foreach (DictionaryEntry entry in propertyEntries) { string name = (string)entry.Key; PersonalizablePropertyEntry propEntry = (PersonalizablePropertyEntry)entry.Value; if (excludeSensitive && propEntry.IsSensitive) { continue; } if ((scope == PersonalizationScope.User) && (propEntry.Scope == PersonalizationScope.Shared)) { continue; } if (propertyBag == null) { propertyBag = new HybridDictionary(propertyEntries.Count, /* caseInsensitive */ false); } object value = FastPropertyAccessor.GetProperty(control, name, control.DesignMode); propertyBag[name] = new Pair(propEntry.PropertyInfo, value); } } if (propertyBag == null) { propertyBag = new HybridDictionary(/* caseInsensitive */ false); } return(propertyBag); }
private static IDictionary GetPersonalizedProperties(Control control, IDictionary personalizableProperties, IDictionary defaultPropertyState, IDictionary initialPropertyState, System.Web.UI.WebControls.WebParts.PersonalizationScope scope) { if (personalizableProperties.Count == 0) { return(null); } bool flag = scope == System.Web.UI.WebControls.WebParts.PersonalizationScope.User; IDictionary dictionary = null; foreach (DictionaryEntry entry in personalizableProperties) { PersonalizablePropertyEntry entry2 = (PersonalizablePropertyEntry)entry.Value; if (!flag || (entry2.Scope != System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared)) { PropertyInfo propertyInfo = entry2.PropertyInfo; string key = (string)entry.Key; object objA = FastPropertyAccessor.GetProperty(control, key, control.DesignMode); bool flag2 = true; if (((initialPropertyState == null) || !initialPropertyState.Contains(key)) && (defaultPropertyState != null)) { object objB = defaultPropertyState[key]; if (object.Equals(objA, objB)) { flag2 = false; } } if (flag2) { if (dictionary == null) { dictionary = new HybridDictionary(personalizableProperties.Count, false); } dictionary[key] = objA; } } } return(dictionary); }
public string FastProperties() { FastPropertyAccessor.SetValue(this.prop, this.obj, "abc"); return((string)FastPropertyAccessor.GetValue(this.prop, this.obj)); }
private void ExtractTemplateValuesRecursive(ArrayList subBuilders, OrderedDictionary table, Control container) { foreach (object subBuilderObject in subBuilders) { ControlBuilder subBuilderControlBuilder = subBuilderObject as ControlBuilder; if (subBuilderControlBuilder != null) { ICollection entries; // filter out device filtered bound entries that don't apply to this device if (!subBuilderControlBuilder.HasFilteredBoundEntries) { entries = subBuilderControlBuilder.BoundPropertyEntries; } else { Debug.Assert(subBuilderControlBuilder.ServiceProvider == null); Debug.Assert(subBuilderControlBuilder.TemplateControl != null, "TemplateControl should not be null in no-compile pages. We need it for the FilterResolutionService."); ServiceContainer serviceContainer = new ServiceContainer(); serviceContainer.AddService(typeof(IFilterResolutionService), subBuilderControlBuilder.TemplateControl); try { subBuilderControlBuilder.SetServiceProvider(serviceContainer); entries = subBuilderControlBuilder.GetFilteredPropertyEntrySet(subBuilderControlBuilder.BoundPropertyEntries); } finally { subBuilderControlBuilder.SetServiceProvider(null); } } string previousControlName = null; bool newControl = true; Control control = null; foreach (BoundPropertyEntry entry in entries) { // Skip all entries that are not two-way if (!entry.TwoWayBound) { continue; } // Reset the "previous" Property Entry if we're not looking at the same control. // If we don't do this, Two controls that have conditionals on the same named property will have // their conditionals incorrectly merged. if (String.Compare(previousControlName, entry.ControlID, StringComparison.Ordinal) != 0) { newControl = true; } else { newControl = false; } previousControlName = entry.ControlID; if (newControl) { control = container.FindControl(entry.ControlID); if (control == null || !entry.ControlType.IsInstanceOfType(control)) { Debug.Assert(false, "BoundPropertyEntry is of wrong control type or couldn't be found. Expected " + entry.ControlType.Name); continue; } } string propertyName; // map the property in case it's a complex property object targetObject = PropertyMapper.LocatePropertyObject(control, entry.Name, out propertyName, InDesigner); // FastPropertyAccessor uses ReflectEmit for lightning speed table[entry.FieldName] = FastPropertyAccessor.GetProperty(targetObject, propertyName, InDesigner); } ExtractTemplateValuesRecursive(subBuilderControlBuilder.SubBuilders, table, container); } } }