Last active
August 29, 2015 13:58
-
-
Save forcewake/10348443 to your computer and use it in GitHub Desktop.
Extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq.Expressions; | |
| using System.Reflection; | |
| namespace Expression.Runner | |
| { | |
| /// <summary> | |
| /// Extensions to <see cref="LambdaExpression"/> | |
| /// </summary> | |
| public static class ExpressionExtensions | |
| { | |
| /// <summary> | |
| /// Returns the <see cref="PropertyInfo"/> referenced by the expression | |
| /// </summary> | |
| /// <param name="expression"></param> | |
| /// <returns></returns> | |
| public static PropertyInfo AsPropertyInfo(this LambdaExpression expression) | |
| { | |
| PropertyInfo info = null; | |
| MemberExpression memberExpression = null; | |
| if (expression.Body.NodeType == ExpressionType.Convert) | |
| { | |
| var body = (UnaryExpression)expression.Body; | |
| memberExpression = body.Operand as MemberExpression; | |
| } | |
| else if (expression.Body.NodeType == ExpressionType.MemberAccess) | |
| { | |
| memberExpression = expression.Body as MemberExpression; | |
| } | |
| if (memberExpression != null) | |
| { | |
| info = (PropertyInfo)memberExpression.Member; | |
| } | |
| return info; | |
| } | |
| /// <summary> | |
| /// Returns the name of the property referenced by the expression | |
| /// </summary> | |
| /// <param name="expression"></param> | |
| /// <returns></returns> | |
| public static string AsPropertyName(this LambdaExpression expression) | |
| { | |
| PropertyInfo info = expression.AsPropertyInfo(); | |
| string propertyName = info == null ? null : info.Name; | |
| return propertyName; | |
| } | |
| public static T GetDefaultValue<T>() | |
| { | |
| // We want an Func<T> which returns the default. | |
| // Create that expression here. | |
| Expression<Func<T>> e = Expression.Lambda<Func<T>>( | |
| // The default value, always get what the *code* tells us. | |
| Expression.Default(typeof(T)) | |
| ); | |
| // Compile and return the value. | |
| return e.Compile()(); | |
| } | |
| public static object GetDefaultValue(this Type type) | |
| { | |
| // Validate parameters. | |
| if (type == null) throw new ArgumentNullException("type"); | |
| // We want an Func<object> which returns the default. | |
| // Create that expression here. | |
| Expression<Func<object>> e = Expression.Lambda<Func<object>>( | |
| // Have to convert to object. | |
| Expression.Convert( | |
| // The default value, always get what the *code* tells us. | |
| Expression.Default(type), typeof(object) | |
| ) | |
| ); | |
| // Compile and return the value. | |
| return e.Compile()(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Characterization.Tests.DataAcess.Core.Extensions | |
| { | |
| using System; | |
| using System.ComponentModel.DataAnnotations.Schema; | |
| using System.Linq; | |
| using System.Reflection; | |
| public static class TypeExtensions | |
| { | |
| /// <summary> | |
| /// Get properties of a type that do not have the 'NotMapped' attribute | |
| /// </summary> | |
| /// <param name="t">Type to examine for properites</param> | |
| /// <returns>Array of properties that can be filled</returns> | |
| public static PropertyInfo[] GetMappedProperties(this Type t) | |
| { | |
| var properties = t.GetProperties(); | |
| var onlyMappedProperties = properties | |
| .Where(propertyinfo => GetAttribute<NotMappedAttribute>(propertyinfo) == null) | |
| .Select(p => p); | |
| return onlyMappedProperties.ToArray(); | |
| } | |
| /// <summary> | |
| /// Get an attribute for a type | |
| /// </summary> | |
| /// <typeparam name="T"></typeparam> | |
| /// <param name="type"></param> | |
| /// <returns></returns> | |
| public static T GetAttribute<T>(this Type type) | |
| where T : Attribute | |
| { | |
| var attributes = type.GetCustomAttributes(typeof(T), false).FirstOrDefault(); | |
| return (T)attributes; | |
| } | |
| /// <summary> | |
| /// Get an attribute for a property | |
| /// </summary> | |
| /// <typeparam name="T"></typeparam> | |
| /// <param name="propertyinfo"></param> | |
| /// <returns></returns> | |
| public static T GetAttribute<T>(this PropertyInfo propertyinfo) | |
| where T : Attribute | |
| { | |
| var attributes = propertyinfo.GetCustomAttributes(typeof(T), false).FirstOrDefault(); | |
| return (T)attributes; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment