反射: 通过 System.Reflection 命名空间中的类以及 System..::.Type,您可以获取有关已加载的程序集和在其中定义的类型(如类、接口和值类型)的信息。您也可以使用反射在运行时创建类型实例,以及调用和访问这些实例。

namespace _Net的反射
{
    
 
    public class Person {
        public void Action() {
            Console.WriteLine("People Run!");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int times = 500000;
            var p = new Person();
            //获得类型的几种方法
            
            Type t = typeof(Person);
            t = Type.GetType("_Net的反射.Person");
            t = p.GetType();
 
            //实例化对象的各种方法
            Stopwatch stop = new Stopwatch(); stop.Start();
            for (int i = 0; i < times; i++)
            {
                var per = new Person();
            }
            stop.Stop();
            Console.WriteLine("直接实例化耗时"+System.Environment.NewLine+stop.ElapsedMilliseconds);
            stop.Reset();
 
            stop.Start();
            for (int i = 0; i < times; i++)
            {
                var per = Activator.CreateInstance(typeof(Person));
            }
            stop.Stop();
            Console.WriteLine("Activator.CreateInstance(typeof(Person))实例化耗时" + System.Environment.NewLine + stop.ElapsedMilliseconds);
            stop.Reset();
 
            stop.Start();
            for (int i = 0; i < times; i++)
            {
                var per = typeof(Person).GetConstructor(Type.EmptyTypes).Invoke(null);
            }
            stop.Stop();
            Console.WriteLine("typeof(Person).GetConstructor(Type.EmptyTypes).Invoke(null)实例化耗时" + System.Environment.NewLine + stop.ElapsedMilliseconds);
            stop.Reset();
 
            stop.Start();
            for (int i = 0; i < times; i++)
            {
                var per =Type.GetType("_Net的反射.Person").GetConstructor(Type.EmptyTypes).Invoke(null);
            }
            stop.Stop();
            Console.WriteLine("Type.GetType(_Net的反射.Person).GetConstructor(Type.EmptyTypes).Invoke(null)实例化耗时" + System.Environment.NewLine + stop.ElapsedMilliseconds);
            stop.Reset();
 
            
            stop.Start();
            for (int i = 0; i < times; i++)
            {
                var per = Activator.CreateInstance(Type.GetType("_Net的反射.Person"));
            }
            stop.Stop();
            Console.WriteLine(" Activator.CreateInstance(Type.GetType(_Net的反射.Person))实例化耗时" + System.Environment.NewLine + stop.ElapsedMilliseconds);
            stop.Reset();
 
            //stop.Start();
            //for (int i = 0; i < times; i++)
            //{
            //    var per = System.Reflection.Assembly.Load("_Net的反射").CreateInstance("_Net的反射.Person");
            //}
            //stop.Stop();
            //Console.WriteLine(" Assembly.Load(_Net的反射).CreateInstance(_Net的反射.Person)实例化耗时" + System.Environment.NewLine + stop.ElapsedMilliseconds);
            //stop.Reset();
 
            stop.Start();
            var la=GetCreateFunc();
            for (int i = 0; i < times; i++)
            {
                var per = la();
            }
            stop.Stop();
            Console.WriteLine(" Func<Person> GetCreateFunc()实例化耗时" + System.Environment.NewLine + stop.ElapsedMilliseconds);
            stop.Reset();
 
            Console.ReadKey();
        }
 
        
        //表达式实例化对象
        static Func<Person> GetCreateFunc() {
            var newExpression = Expression.New(typeof(Person));
            return Expression.Lambda<Func<Person>>(newExpression, null).Compile();
        }
 
    }
}

image

 

至于EMIT太复杂啦. 个人不去研究了哭泣的脸

posted @ 2011-07-06 17:46 MyCoolDog 阅读(45) 评论(0) 编辑

网站结构

image

webconfig

设置为form验证, 并拒绝所有的匿名用户
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Index" timeout="2880" path="/" />
    </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>

如果我们徐凯开放首页比如说Home/Index,那么做如下配置.  如果是Home文件夹下所有的页面都能访问, 那么 path=”Home”即可

<location path="Home/Index">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>

cookie

启动程序, 来到登录页面. 如果登录成功, 那么我们需要写入cookie.

登陆页面

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<mvc安全验证.Models.User>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Index</title>
</head>
<body>
    <div>
         <% using( Html.BeginForm()){%>
         登录
         <%: Html.TextBoxFor(m => m.UserName, new { @class = "log" })%>
         <%: Html.TextBoxFor(x => x.RealName) %><br />
         <input type="submit" value="login" />
         <%};%>
    </div>
</body>
</html>
处理方法
[HttpPost]
        public ActionResult Index(Models.User model) {
            if (model.UserName == "admin")
            {
                //创造票据
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(model.UserName, false, 1);
                //加密票据
                string ticString = FormsAuthentication.Encrypt(ticket);
                //输出到客户端
                Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, ticString));
                //跳转到登录前页面
                return Redirect(HttpUtility.UrlDecode( Request.QueryString["ReturnUrl"]));
            }
            return View();
        }
退出.
通过 new FormsAuthenticationTicket(model.UserName, false, 时长); 设置.AXPXAUTH过期时长. 但是如果new HttpCookie(FormsAuthentication.FormsCookieName, ticString) 这个cookie对象没有设置过期时间, 那么上面设置的时长再长, cookie的生命周期还是浏览器的生命周期.
public ActionResult Logout() {
            FormsAuthentication.SignOut();
            return Redirect(FormsAuthentication.LoginUrl);
        }

这样, 在默认首页 home/index 中可以得到image

八卦一下. User的值是在哪里获得的呢?我们加载进来一个DLL, 自定义的httpmodulehttp://www.cnblogs.com/jianjialin/archive/2011/06/14/2080880.html

跟踪一下. 发现在application_AuthenticateRequest事件里面, 我们可以获得User对象了.

代码下载: 群共享搜索 mvc安全验证

posted @ 2011-07-06 15:27 MyCoolDog 阅读(327) 评论(0) 编辑