最近项目遇到需要读取RedisSessionStateProvider配置文件applicationName节点,如:
读取的方法有很多:
1直接读取web.config文件,
void test1() { SessionStateSection config = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState"); string providerName = config.CustomProvider; ProviderSettings ps = config.Providers[providerName]; string str = ps.Parameters["applicationName"]; }
2通过SessionStateStoreProviderBase来访问对象属性, 可惜的是对应的属性及类都是内部的,所以我们需要用到反射。
所以编码为如下:
void test2() { var type = typeof(RedisSessionStateProvider); //获取RedisSessionStateProvider的静态configuration字段 var configField = type.GetField("configuration", BindingFlags.Static | BindingFlags.NonPublic); var configValue = configField.GetValue(new RedisSessionStateProvider()); type = configField.FieldType; //获取configuration的ApplicationName属性 var prop = type.GetProperty("ApplicationName"); var obj = prop.GetValue(configValue).ToString(); } T GetSessionStateStoreProviderBase() where T : SessionStateStoreProviderBase, new() { SessionStateSection config = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState"); string providerName = config.CustomProvider; ProviderSettings ps = config.Providers[providerName]; ProviderBase provide = ProvidersHelper.InstantiateProvider(ps, typeof(T)); return (provide as T); }
3 次方法和方法2一样好,直接放问实例或对象的属性,ProviderConfiguration和configuration属性都是内部的,我们可以不通过修改源码(或者用mono ceil 来修改dll文件)使ProviderConfiguration类和configuration属性成为public。 那么code就很简单了
string applicationName1 = RedisSessionStateProvider.configuration.ApplicationName;
4 用mono ceil 来修改dll文件,方法3是找到源码直接修改,这里方法4是没有源码,用monon修改的。我们新建一个控制台应用程序,添加
using System; using System.IO; using System.Linq; using Mono.Cecil; class Program { static void Main(string[] args) { string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Microsoft.Web.RedisSessionStateProvider.dll"); AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(filePath); TypeDefinition[] types = asm.MainModule.Types.ToArray(); //修改ProviderConfiguration为public TypeDefinition typeConfiguration = types.FirstOrDefault(x => x.Name == "ProviderConfiguration"); typeConfiguration.IsPublic = true; //修改ProviderConfiguration的字段为public TypeDefinition typeRedisProvide = types.FirstOrDefault(x => x.Name == "RedisSessionStateProvider"); FieldDefinition filedConfiguration = typeRedisProvide.Fields.ToArray().FirstOrDefault(x => x.Name == "configuration"); filedConfiguration.IsPublic = true; //修改程序集的签名 foreach (ModuleDefinition item in asm.Modules) { var assemblyName = item.Assembly.Name; assemblyName.PublicKey = GetPublicKey(); assemblyName.PublicKeyToken = GetPubliKeyToken(); } //保存dll文件 filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Microsoft.Web.RedisSessionStateProvider2.dll"); asm.Write(filePath); //Console.ReadLine(); } private static byte[] GetPublicKey() { string s = @"00240000048000009400000006020000002400005253413100040000010001005b0b4a219a91bee970338aadc8890eb73db3a87500c9b82cf605bebdb64f74769a213fbdf0476641c728281ffb5bb700c7f0a55f5407bfe984eb4abfd947ddbc6476c8ea4c5207421750dc2385fd99163cbb12df87190793552334614985258c830b8d960b5e21c5a0cc6be0941b3d1777c8a09d22db62ed59e5540822c6289b"; return StrToHexBytes(s); } private static byte[] GetPubliKeyToken() { string s = "f68305d2ab29aee8"; return StrToHexBytes(s); } private static byte[] StrToHexBytes(string hexString) { hexString = hexString.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString += " "; byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); return returnBytes; } }
先修改Microsoft.Web.RedisSessionStateProvider.dll 文件 另存为Microsoft.Web.RedisSessionStateProvider2.dll,在放到特定目录下 改为Microsoft.Web.RedisSessionStateProvider.dll 对其修改签名。
代用方式和方法3一样 就一行code string applicationName1 = RedisSessionStateProvider.configuration.ApplicationName;
http://blog.csdn/ma_jiang/article/details/7936235
整个项目需要用nuget 来安装Microsoft.Web.RedisSessionStateProvider。至于用mono ceil 来修改dll文件, 网上有你很多, 一定给要记住修改签名哦哦。