Differentiate class instance from "instance" interface

5

Is there any way to differentiate% w / o% w / o% w / o in the code below,% w / o% or another method?

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

I want to execute a method only if the v1 is "instantiated" from v2 , in the above example it would only be possible to call a function if passed as parameter reflection

Another way that would solve the problem would be to prevent a method from having input parameter variável not accepting Interface .

    
asked by anonymous 25.06.2015 / 04:16

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 , so v1 has field campoMinhaClasse

Fields: Test v2

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

= true , so v2 has field campoMinhaClasse

Methods: Test v1

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

= true , so v1 has method metodoQueExisteSomenteMinhaClasse

Methods: Test v2

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

= true , so v2 has method metodoQueExisteSomenteMinhaClasse

Conclusion

So NO is how to structurally differentiate the instance of v1 from v2 .

    
25.06.2015 / 04:25