C# 类型转换 怎么存储一个自定义类型 到Session中

我有一个自定义OpenAPI类型的变量

 OpenAPI api = new OpenAPI(HttpContext.Current.Session["urlv1"].ToString(), new Credentials
            {
                AppKey = HttpContext.Current.Session["appkey"].ToString(),
                AppSecret = HttpContext.Current.Session["appsecret"].ToString(),
                UserName = HttpContext.Current.Session["username"].ToString(),
                Password = HttpContext.Current.Session["password"].ToString(),
                LoginDate = HttpContext.Current.Session["logindate"].ToString(),
                AccountNumber = HttpContext.Current.Session["accountnumber"].ToString()
            });

我用Seesion 存储这个变量

 OpenAPI api = (OpenAPI)HttpContext.Current.Session["api"];

用显示转换给api赋值却为空请问怎么取到session的值

session里存的是object类型,所以你想往里存什么都行
为空是因为你这只有定义和取值,赋值呢?你没给api赋值啊
赋值是HttpContext.Current.Session["api"]=api

将OpenAPI转换成json字符串,在存入session中,取的时候也是将json字符串反序列化成OpenAPI。