예제 #1
0
        public virtual void Define <T>(string variation, Expression <Func <T> > definition, Action <T> afterCreation = null) where T : class
        {
            if (_costructors.ContainsKey <T>(variation))
            {
                throw new DuplicateBlueprintException(typeof(T), variation);
            }

            switch (definition.Body.NodeType)
            {
            case ExpressionType.MemberInit:
            {
                var memberInitExpression = ((MemberInitExpression)definition.Body);
                _costructors.Add <T>(variation, memberInitExpression.NewExpression);
                _properties.Add <T>(variation, memberInitExpression.Bindings);
            }
            break;

            case ExpressionType.New:
            {
                _costructors.Add <T>(variation, (NewExpression)definition.Body);
            }
            break;

            default:
                throw new ExpressionTypeNotSupportedException();
            }

            if (afterCreation != null)
            {
                _postCreationActions.Add(variation, afterCreation);
            }
        }