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)
andif (v2 is MinhaClasse)
are always true. 2 - method.GetInterfaces()
there is no 3 - Always true– rubStackOverflow
It’s a peculiar question, since to be able to say
IMinhaClasse v2 = new MinhaClasse()
, thenMinhaClasse
should always implementIMinhaClasse
, then I believe it is impossible to differentiate them the way you intend– Felipe Douradinho
Indeed, however, in
IMinhaClasse
for example I may haveatributos privados (somente com get)
already inMinhaClasse
have full accessget; set;
. The reason for this is because I am using an ORM that requiresMinhaClasse
has the public and accessible methodsget; set;
. So I want to useIMinhaClasse
to "minimize" this problem.– rubStackOverflow
Oh yes, I get it. But by Reflection you can get the attributes yes
– Felipe Douradinho
Really, there is NO way. I will change my answer with the tests.
– Felipe Douradinho
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.
– rubStackOverflow
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 classMinhaClasse
.– Felipe Douradinho