Differentiate interface "instance" class instance

Asked

Viewed 608 times

5

Got some way of differentiating v1 of v2 in the code below, by reflection or other method?

var v1 = new MinhaClasse();
IMinhaClasse v2 = new MinhaClasse();

I want to run a method only if a variável is "instantiated" from the Interface, in the above example it would only be possible to call a function if it passed as parameter v2

Another way that would solve the problem would be to avoid a method having as input parameter IMinhaClasse unaccepted MinhaClasse.

1 answer

1


There’s no way.

IMinhaClasse.cs

interface IMinhaClasse
{
    void IMinhaClasse();
}

MinhaClasse.cs

class MinhaClasse : IMinhaClasse
{

    public String campoMinhaClasse;

    public void metodoQueExisteSomenteMinhaClasse()
    {

    }

    public void IMinhaClasse()
    {

    }
}

Form1.cs

var v1 = new MinhaClasse();
IMinhaClasse v2 = new MinhaClasse();

Fields: Test v1

v1.GetType().GetField("campoMinhaClasse") != null

= true, therefore, v1 has the field campoMinhaClasse

Fields: Test v2

v2.GetType().GetField("campoMinhaClasse") != null

= true, therefore, v2 has the field campoMinhaClasse

Methods: Testing v1

v1.GetType().GetMethod("metodoQueExisteSomenteMinhaClasse") != null

= true, therefore, v1 has the method metodoQueExisteSomenteMinhaClasse

Methods: Testing v2

v2.GetType().GetMethod("metodoQueExisteSomenteMinhaClasse") != null

= true, therefore, v2 has the method metodoQueExisteSomenteMinhaClasse

Completion

Therefore, NAY there is how to structurally differentiate the instance of v1 of v2.

  • I tested the answers but they didn’t work: 1 - if (v1 is IMinhaClasse) and if (v2 is MinhaClasse) are always true. 2 - method .GetInterfaces() there is no 3 - Always true

  • 1

    It’s a peculiar question, since to be able to say IMinhaClasse v2 = new MinhaClasse(), then MinhaClasse should always implement IMinhaClasse, then I believe it is impossible to differentiate them the way you intend

  • Indeed, however, in IMinhaClasse for example I may have atributos privados (somente com get) already in MinhaClasse have full access get; set;. The reason for this is because I am using an ORM that requires MinhaClasse has the public and accessible methods get; set;. So I want to use IMinhaClasse to "minimize" this problem.

  • Oh yes, I get it. But by Reflection you can get the attributes yes

  • 1

    Really, there is NO way. I will change my answer with the tests.

  • In your conclusion you meant that there is no way to differentiate directly, only comparing the content of the classes is that? Explain the conclusion better.

  • Wow, I fixed it, I spelled it wrong. Really there is no way to differentiate if it is instance of Iminhaclasse or Minhaclasse...if it were to distinguish from which interfaces a class implements(s) etc, then it would be simple..., var is the same as the base/interface class MinhaClasse.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.