Esempio n. 1
0
        static ResponseManager()
        {
            _endpointToResponseMap = new Dictionary <Endpoint, List <ResponseMatch> >();

            Assembly assembly = typeof(IResponse).GetTypeInfo().Assembly;

            foreach (Type responseType in assembly.ExportedTypes.Where(x => x.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IResponse))))
            {
                EndpointAttribute endpointAttribute =
                    responseType.GetTypeInfo().GetCustomAttributes(typeof(EndpointAttribute), false)
                    .OfType <EndpointAttribute>()
                    .FirstOrDefault();
                if (endpointAttribute != null)
                {
                    List <ResponseMatch> responseMatches;
                    if (_endpointToResponseMap.TryGetValue(endpointAttribute.Endpoint, out responseMatches) == false)
                    {
                        _endpointToResponseMap.Add(endpointAttribute.Endpoint,
                                                   responseMatches = new List <ResponseMatch>());
                    }

                    //TODO: What happens if there is not a default constructor? multiple constructors?
                    Expression body = Expression.New(responseType);
                    var        func = (Func <IResponse>)Expression.Lambda(body).Compile();

                    responseMatches.Add(new ResponseMatch(func, endpointAttribute.GetPredicate()));
                }
            }
        }