Ejemplo n.º 1
0
        public static Accessor FindPropertyByName <TEntity>(string propertyName)
        {
            var propertyToFind = propertyName;
            var prefix         = typeof(TEntity).Name;

            if (propertyToFind.StartsWith(prefix))
            {
                propertyToFind = propertyToFind.Substring(prefix.Length);
            }

            //This is to handle the situation with address where we have properties that start with the type name.
            //We won't find any properties for 1 on address when we are looking for address1 and then rip off the type name.
            var property = TypeDescriptorCache.GetPropertyFor(typeof(TEntity), propertyToFind) ??
                           TypeDescriptorCache.GetPropertyFor(typeof(TEntity), propertyName);

            if (property != null)
            {
                return(new SingleProperty(property));
            }

            if (ExtensionProperties.HasExtensionFor(typeof(TEntity)))
            {
                var extensionType         = ExtensionProperties.ExtensionFor(typeof(TEntity));
                var extensionProperty     = TypeDescriptorCache.GetPropertyFor(extensionType, propertyToFind);
                var extensionAccessorType = typeof(ExtensionPropertyAccessor <>).MakeGenericType(extensionType);
                return((Accessor)Activator.CreateInstance(extensionAccessorType, extensionProperty));
            }

            return(null);
        }