Exemplo n.º 1
0
    public static AbilityBase GeneAbilityBase(KeyValuePair <string, Dictionary <string, object> > abilityInfo)
    {
        AbilityBase abilityBase = new AbilityBase();

        abilityBase._name = abilityInfo.Key;
        Type type = typeof(AbilityBase);
        Dictionary <string, object> abilityFeilds = abilityInfo.Value;

        foreach (string feildName in abilityFeilds.Keys)
        {
            PropertyInfo propertyInfo = type.GetProperty(feildName);
            if (propertyInfo == null)
            {
                Debug.Log(string.Format("Illegal field:ability feild '{0}' does not exist", feildName));
                throw new System.FieldAccessException(string.Format("Illegal field:ability feild '{0}' does not exist", feildName));
            }
            object valueOb = abilityFeilds[feildName];
            if (propertyInfo.PropertyType.IsEnum)
            {
                try{
                    System.Convert.ToInt32(valueOb);
                }catch {
                    valueOb = Enum.Parse(propertyInfo.PropertyType, (string)valueOb);
                }
                if (!Enum.IsDefined(propertyInfo.PropertyType, valueOb))
                {
                    Debug.Log(string.Format("Illegal {0} value :{1}", propertyInfo.PropertyType, valueOb));
                    throw new System.ArgumentException(string.Format("Illegal {0} value :{1}", propertyInfo.PropertyType, valueOb));
                }
            }
//			Debug.Log(propertyInfo.Name);
//			Debug.Log(valueOb.GetType()+","+propertyInfo.PropertyType);
            if (valueOb.GetType() != propertyInfo.PropertyType)
            {
                valueOb = System.Convert.ChangeType(valueOb, propertyInfo.PropertyType);
            }
            propertyInfo.SetValue(abilityBase, valueOb, null);
        }
        if (!abilityBase.CheckInstanceFields())
        {
            throw new System.ArgumentException(string.Format("The ability data is deficient in ability that named:{0}", abilityBase.name));
        }
        return(abilityBase);
    }