示例#1
0
        static void BuildRequestContentMethod(Type t, BuilderContextItem item)
        {
            //get interface type
            var iface = t.GetInterface(typeof(IRequestContentBuilder <>).FullName);

            if (iface == null)
            {
                return;
            }
            var dataType = iface.GetGenericArguments()[0];

            if (item.BuilderObject == null)
            {
                item.BuilderObject = Activator.CreateInstance(t);
            }

            var p1         = Expression.Parameter(typeof(object), "p1");
            var p2         = Expression.Parameter(typeof(RequestWrapRequestContentEventArgs), "p2");
            var callMethod = Expression.Call(
                Expression.Constant(item.BuilderObject),
                t.GetMethod("BuildRequestContent"),
                Expression.Convert(p1, dataType),
                p2
                );

            item.CachedWrapMethod = Expression.Lambda <Func <object, RequestWrapRequestContentEventArgs, HttpRequestContent> >(callMethod, p1, p2).Compile();
        }
示例#2
0
        static BuilderContextItem GetBuilderContextItem(Type type)
        {
            BuilderContextItem item;

            if (_contextCache.TryGetValue(type, out item))
            {
                return(item);
            }
            lock (_contextCache)
            {
                if (_contextCache.TryGetValue(type, out item))
                {
                    return(item);
                }

                var attributes = type.GetCustomAttributes(true);

                foreach (var attribute in attributes)
                {
                    if (attribute is RequestContentBuilderAttribute)
                    {
                        BuildRequestContentMethod((attribute as RequestContentBuilderAttribute).Type, item ?? (item = new BuilderContextItem()));
                    }
                    else if (attribute is ResponseContentBuilderAttribute)
                    {
                        BuildResponseContentMethod((attribute as ResponseContentBuilderAttribute).Type, item ?? (item = new BuilderContextItem()));
                    }
                }

                _contextCache.Add(type, item);
            }

            return(item);
        }