码迷,mamicode.com
首页 > 其他好文 > 详细

反射和特性在SOA中的使用

时间:2014-12-25 15:59:42      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

组长给了个任务,需要了解SOA,眼一瞎,什么都不懂。幸好摆弄过百度API,加上百度了一个不错的文章,算有所了解了吧!

(一开始以为什么都没有,自己瞎捉摸,后来才知道原来已经有了一个demo,我也是醉了。)

不过自己写了个简单到不能再简单的,也不知道是对是错的小demo,然后再看别人的,也算是加深学习了。

 用一般处理程序来做接口:

技术分享
[Test(ClassType = typeof(TestSOABusiness))]
    public class TestHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string usertype = context.Request.QueryString["usertype"];
            Type classtype = null;
            string value=string.Empty;
            Type t = this.GetType();
            object[] objAttrs = t.GetCustomAttributes(typeof(TestAttribute), true);
            foreach (object obj in objAttrs)
            {
                TestAttribute attr = obj as TestAttribute;
                if (attr != null)
                {
                   classtype = attr.ClassType;
                   break;
                }
            }

            switch(usertype)
            {
                case "GetMethodA":
                    {
                        MethodInfo mf1 = classtype.GetMethod(usertype, BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string) }, null);
                        value = (string)mf1.Invoke(null, new object[] { usertype });
                        break;
                    }
                case "GetMethodB":
                    {
                        MethodInfo mf1 = classtype.GetMethod(usertype, BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(int) }, null);
                        value = (string)mf1.Invoke(null, new object[] { 2 });
                        break;
                    }
                default:
                    break;

            }
            context.Response.Write(value);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
View Code

 自定义的方法和特性就不贴了。

ps:调用方法的参数实体也是可以利用反射和配置文件来完成的,一是麻烦,二是自己一开始也不会(现在也不怎么会,只是知道了大概的逻辑。。。)。

 

反射和特性在SOA中的使用

标签:

原文地址:http://www.cnblogs.com/Cheer137/p/4184746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!