Type inference of generic extension methods and static abstract interfaces #6479
Answered
by
ufcpp
Notheisz57
asked this question in
Language Ideas
-
Given the following interface and extension method: public interface IClaim<TValue>
{
public abstract static string Type { get; }
public abstract static string Issuer { get; }
public IReadOnlyList<TValue> Value { get; init; }
public abstract static TValue Deserialize(string valueStr);
public abstract static string Serialize(TValue value);
}
using System.Security.Claims;
public static partial class ClaimsExtensions
{
public static ClaimsIdentity Add<TClaim, TValue>(this ClaimsIdentity identity, TValue value) where TClaim : IClaim<TValue>
{
identity.AddClaim(new Claim(TClaim.Type, TClaim.Serialize(value), null, TClaim.Issuer));
return identity;
}
// Omitted similar extension methods (GetClaim, HasClaim, etc.)
} The consumer/developer provides an implementation like this: public class PrincipalGuidClaim : IClaim<Guid> /* Additional classes/interfaces omitted */
{
public static string Issuer => "http://mysite.com";
public static string Type => $"schema://mysite.com/claims/PrincipalGuid";
public IReadOnlyList<Guid> Value { get; init; }
public static Guid Deserialize(string valueStr) => Guid.Parse(valueStr);
public static string Serialize(Guid value) => value.ToString();
} In a perfect world, the consumer/developer would use it like this: // using System.Security.Claims;
ClaimsIdentity identity = new ClaimsIdentity() { Label = "schema://mysite.com/Identity" };
identity.Add<PrincipalGuidClaim>(userGuid /* decl. omitted */); // CS1061 - 'ClaimsIdentity' does not contain a definition for 'Add'... But in the real world, the consumer/developer must provide both type parameters like this: // using System.Security.Claims;
ClaimsIdentity identity = new ClaimsIdentity() { Label = "schema://mysite.com/Identity" };
identity.Add<PrincipalGuidClaim, Guid>(userGuid /* decl. omitted */); Am I missing something here? |
Beta Was this translation helpful? Give feedback.
Answered by
ufcpp
Sep 22, 2022
Replies: 1 comment
Answer selected by
svick
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#339
#1349