jump to navigation

Interfaces & Reflection April 23, 2006

Posted by Christian López Espínola in CSharp, Programming.
trackback

Recently, at work, we needed to know if a class implemented an interface by using Reflection. I was selected to do this research, and it was very easy. This example needs no description:

class Program
{
static void Main (string [] args)
{
Type cocaType = typeof(Coca);
string interfaceNeeded = "iveneno";
Type interfaceType = cocaType.GetInterface(interfaceNeeded, true);
if (interfaceType != null)
{
Console.WriteLine(cocaType.Name + " implements " + interfaceType.Name);
}
else
{
Console.WriteLine(cocaType.Name + " doesn't implement " + interfazBuscada);
}

Console.ReadLine();
}
}

public interface IVeneno
{
void Envenenarse ();
}

public interface IDroga : IVeneno
{
void Chutarse ();
}

public class Hachis : IDroga
{
public void Chutarse ()
{
Console.WriteLine("Fumate un porro de hachi amparo");
}
public void Envenenarse ()
{
Chutarse();
}
}
public class Coca : IDroga
{
public void Chutarse ()
{
Console.WriteLine("Tiene un pinxo a mano?");
}
public void Envenenarse ()
{
Chutarse();
}
}

Comments»

1. Vio - April 23, 2006

Ohh, yeah, when developers rocks, sofware roles:

I read it in some shirt.

Jah, Jah.

2. Michael Sync - November 15, 2006

How did u use “code” tag in ur post?
I also wanna do the same..
I posted one article abt Yahoo calendar in my blog.
but all coding are not good to look.. :(

3. Christian López Espínola - November 15, 2006

With the HTML Tag code. If doesn’t works like you want, look up in your theme css.

4. Michael Sync - November 15, 2006

Oki. thanks.