azrosen92
azrosen92

Reputation: 9117

Creating an instance of a interface in c#

I am using an interface in C# and rather than write an entirely new class which implements that interface is it possible to just create an object which implements that interface? The interface is defined as

public interface ITokenStore
{

    IToken CreateRequestToken(IOAuthContext context);


    IToken CreateAccessToken(IOAuthContext context);
}

And I know in java I could something like

ITokenStore tokenStore = new ITokenStore()
    {
        IToken CreateRequestToken(IOAuthContext context) {
            IToken requestToken = null;

            return requestToken;
        }

        IToken CreateAccessToken(IOAuthToken context) {
            IToken accessToken = null;

            return accessToken;
        }
    };

Is there an equivalent way to instantiate in instance of an interface in c#?

Upvotes: 10

Views: 23635

Answers (7)

mkoertgen
mkoertgen

Reputation: 992

As far as i know .NET does not have the Java concept of anonymous inner classes. There is a typical need for dynamic implementations of an interface in unit testing scenarios. So may be a look on some dynamic mocking frameworks may be interesting for you, for instance

  1. Impromptu Interface (includes support for dynamic)
  2. Moq

Have a look on NuGet gallery for packages tagged with mocking.

Upvotes: 2

oakio
oakio

Reputation: 1898

If you really want this:

    private static void Main(string[] args)
    {
        IFoo foo = new IFoo(); // <- interface
        foo.Print();

        Console.ReadKey();
    }


    [ComImport, Guid("A7D5E89D-8FBD-44B7-A300-21FAB4833C00"), CoClass(typeof(Foo))]
    public interface IFoo
    {
        void Print();
    }

    public class Foo : IFoo
    {
        public void Print()
        {
            Console.WriteLine("Hello!");
        }
    }

But if you open compiled assembly on ildasm.exe, you will see:

IL_0001: newobj instance void TestApp.Program/Foo::.ctor()

Upvotes: 5

Interfaces have no logic in them by design. They simply don't actually do anything.

Instantiating one without an implementing class doesn't even make sense

Upvotes: 4

Chad
Chad

Reputation: 7517

Like the others have said, no, this is not possible.

However, you could write one generalized class that has delegates for each method. Something like the following:

public class DelegateTokenStore : ITokenStore
{
    public delegate IToken TokenCreator(IOAuthContext context);
    public TokenCreator RequestTokenCreator { get; set; }
    public TokenCreator AccessTokenCreator { get; set; }

    public IToken CreateRequestToken(IOAuthContext context)
    {
        return RequestTokenCreator(context);
    }

    public IToken CreateAccessToken(IOAuthContext context)
    {
        return AccessTokenCreator(context);
    }
}

Then you could use it like this:

ITokenStore tokenStore = new DelegateTokenStore()
{
    RequestTokenCreator = (context) =>
    {
        IToken requestToken = null;
        return requestToken;
    },
    AccessTokenCreator = (context) =>
    {
        IToken accessToken = null;
        return accessToken;
    },
};

I'm not saying it's necessarily a good idea to take this approach. Like anything, the appropriateness depends on your use case.

Upvotes: 0

Yury Schkatula
Yury Schkatula

Reputation: 5369

Something like this:

public class MyTokenStore: ITokenStore
{
    public IToken CreateRequestToken(IOAuthContext context)
    {
        ...some code here...
    }
    public IToken CreateAccessToken(IOAuthContext context)
    {
        ...some code here...
    }
}

Then use it:

ITokenStore x = new MyTokenStore();

Upvotes: 0

Tim
Tim

Reputation: 15247

You can't create an instance of an interface in C#. You must create an instance of a class/type that implements that interface.

Might want to go read up on interfaces in C#. There's no way to "fake" it like Java does.

I'm kind if intrigued by this concept though since it is something available in another language. I suppose you could use an anonymous class with delegate parameters if your consumer knew how to fake duck typing. But that hardly seems worth it.

Upvotes: 0

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391714

The only way to "Create an instance of a interface in c#" is to create an instance of a type implementing the interface.

Upvotes: 14

Related Questions