context.Get() 和它的通用版本有什么区别?
What's the difference between context.Get() and its generic version?
我正在尝试熟悉 OWIN,但有很多事情让我感到困惑。例如,在部分 startup.cs class 中,我通过
注册上下文回调
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
有什么区别?为什么我们需要那个泛型方法?
我可以这样获取上下文:
context.Get<ApplicationDbContext>())
context.GetUserManager<ApplicationUserManager>()
Get 和 GetUserManager 方法有什么区别?为什么我不能只为 ApplicationUserManager 调用 context.Get?
Get<UserManager>
和GetUserManager<UserManager>
没有区别
这是两者的源代码...
/// <summary>
/// Retrieves an object from the OwinContext using a key based on the AssemblyQualified type name
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context"></param>
/// <returns></returns>
public static T Get<T>(this IOwinContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
return context.Get<T>(GetKey(typeof (T)));
}
/// <summary>
/// Get the user manager from the context
/// </summary>
/// <typeparam name="TManager"></typeparam>
/// <param name="context"></param>
/// <returns></returns>
public static TManager GetUserManager<TManager>(this IOwinContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
return context.Get<TManager>();
}
我正在尝试熟悉 OWIN,但有很多事情让我感到困惑。例如,在部分 startup.cs class 中,我通过
注册上下文回调app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
有什么区别?为什么我们需要那个泛型方法?
我可以这样获取上下文:
context.Get<ApplicationDbContext>())
context.GetUserManager<ApplicationUserManager>()
Get 和 GetUserManager 方法有什么区别?为什么我不能只为 ApplicationUserManager 调用 context.Get?
Get<UserManager>
和GetUserManager<UserManager>
没有区别
这是两者的源代码...
/// <summary>
/// Retrieves an object from the OwinContext using a key based on the AssemblyQualified type name
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context"></param>
/// <returns></returns>
public static T Get<T>(this IOwinContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
return context.Get<T>(GetKey(typeof (T)));
}
/// <summary>
/// Get the user manager from the context
/// </summary>
/// <typeparam name="TManager"></typeparam>
/// <param name="context"></param>
/// <returns></returns>
public static TManager GetUserManager<TManager>(this IOwinContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
return context.Get<TManager>();
}