using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using BFWebManage.Helper; namespace test_20190902 { class Program { static void Main(string[] args) { } void fun1(ActionExecutingContext filterContext) { filterContext.HttpContext.Session.Get<int>("UserInfo"); } } public class ActionExecutingContext { public HttpContext HttpContext { get; set; } } //抽象类 //abstract public class HttpContext { public ISession Session { get; set; } } } namespace BFWebManage.Helper { //属性 扩展类 (属性名+Extensions的静态类) public static class SessionExtensions { public static T Get<T>(this ISession session, string key) { var value = session.GetString(key); //Microsoft.AspNetCore.Http.Extensions.dll return value == null ? default(T) : default(T); //如果T是int,那么默认值default(int)为0 //return value == null ? default(T) : // JsonConvert.DeserializeObject<T>(value); //把字符串转换为对象 } } } namespace Microsoft.AspNetCore.Http { //接口 interface public interface ISession { } public static class SessionExtensions { public static string GetString(this ISession session, string key) { return ""; } } }
//扩展 string方法
public static class A { public static void tets(this string str) { } }