コード例 #1
0
    public static BaseCard GeneBaseCard(KeyValuePair <string, Dictionary <string, object> > cardInfo)
    {
        BaseCard baseCard = new BaseCard();

        baseCard._name = cardInfo.Key;
        Type type = typeof(BaseCard);
        Dictionary <string, object> cardAttr = cardInfo.Value;

        foreach (string attribute in cardAttr.Keys)
        {
            PropertyInfo propertyInfo = type.GetProperty(attribute);
            if (propertyInfo == null)
            {
                Debug.Log(string.Format("Illegal field:Card attribute '{0}' does not exist", attribute));
                throw new System.Exception(string.Format("Illegal field:Card attribute '{0}' does not exist", attribute));
            }
            object valueOb = cardAttr[attribute];
            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));
                }
            }
            if (valueOb.GetType() != propertyInfo.PropertyType)
            {
                valueOb = System.Convert.ChangeType(valueOb, propertyInfo.PropertyType);
            }
            propertyInfo.SetValue(baseCard, valueOb, null);
        }
        if (!baseCard.CheckInstanceFields())
        {
            throw new System.ArgumentException(string.Format("The card data is deficient in card that named:{0}", baseCard.name));
        }
        return(baseCard);
    }