Friday, March 11, 2011

Visual Studio : Automatically generate interface properties and methods

Cam across a neat trick to save you a heap of pain.

If you want to instantiate an interface you need to create a physical "copy".

e.g.


class Dummy : IClaimsIdentity
{
}


IClaimsIdentity is the interface and Dummy is the physical copy. If you try and compile this, the compiler will complain that you haven't defined all the mandatory methods etc.

You could go through the compiler errors one by one or you could right-click on IClaimsIdentity and select Implement Interface / Implement Interface Explicitly.

This generates:


class ICII : IClaimsIdentity
{
IClaimsIdentity IClaimsIdentity.Actor
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

SecurityToken IClaimsIdentity.BootstrapToken
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

ClaimCollection IClaimsIdentity.Claims
{
get { throw new NotImplementedException(); }
}

IClaimsIdentity IClaimsIdentity.Copy()
{
throw new NotImplementedException();
}

string IClaimsIdentity.Label
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

string IClaimsIdentity.NameClaimType
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

string IClaimsIdentity.RoleClaimType
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

string System.Security.Principal.IIdentity.AuthenticationType
{
get { throw new NotImplementedException(); }
}

bool System.Security.Principal.IIdentity.IsAuthenticated
{
get { throw new NotImplementedException(); }
}

string System.Security.Principal.IIdentity.Name
{
get { throw new NotImplementedException(); }
}
}


Much easier to fill in the blanks!

Enjoy!

No comments: