Ejemplo n.º 1
0
        private GallioFunc <object[], TOutput> TryGetMemberAsProperty(FixtureMemberInvokerTargets targets, ITypeInfo ownerInfo)
        {
            if ((targets & FixtureMemberInvokerTargets.Property) != 0)
            {
                IPropertyInfo info = ownerInfo.GetProperty(memberName, bindingFlags);

                if (info != null && info.GetMethod != null)
                {
                    return(args =>
                    {
                        object fixtureInstance = GetFixtureInstance(info.GetMethod.IsStatic);
                        PropertyInfo property = (type == null) ? GetMemberInfo <PropertyInfo>(t => t.GetProperty(memberName, bindingFlags)) : info.Resolve(true);

                        if (property == null)
                        {
                            throw new TestFailedException(String.Format("Could not find property '{0}'.", memberName));
                        }

                        return (TOutput)property.GetValue(fixtureInstance, null);
                    });
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
 public static void ApplyValues(object target, IDictionary <string, object> propertyValues)
 {
     if (propertyValues != null && propertyValues.Count != 0)
     {
         ITypeInfo typeInfo = TypeFactory.GetTypeInfo(target.GetType());
         foreach (KeyValuePair <string, object> propertyValue in propertyValues)
         {
             PropertyInfo property = typeInfo.GetProperty(propertyValue.Key);
             if (property == null)
             {
                 throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Unable to find property {0} on type {1}.", propertyValue.Key, typeInfo.FullName));
             }
             try
             {
                 if (TypeFactory.GetTypeInfo(property.PropertyType).IsEnum)
                 {
                     object value = Enum.Parse(property.PropertyType, propertyValue.Value.ToString(), ignoreCase: true);
                     property.SetValue(target, value, null);
                 }
                 else
                 {
                     property.SetValue(target, propertyValue.Value, null);
                 }
             }
             catch (Exception ex)
             {
                 throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Unable to set property {0} on type {1}: {2}", propertyValue.Key, typeInfo.FullName, ex.Message));
             }
         }
     }
 }