Weblog Of Nirandas | home

Developer From INDIA

Beginning Reflection.Emit, a basic example

clock December 25, 2008 13:49 by author Nirandas

 

The System.Reflection.Emit namespace provides us the tools required to generate .NET code on the fly. Especially the DynamicMethod class makes the job of creating small code snippet easier. In this post I’ll show a simple example of dynamically creating a method which takes a string as its argument and returns a string “hi “prepended to it. First, we will create the DynamicMethod object specifying the argument list and the return

More...

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Determining if a Type implements an interface

clock December 22, 2008 05:08 by author

If we have an object of a type, we can easily check if that object implements a particular type by using is operator.

 

if(o is IExample)

Console.WriteLine("Yes");

 

Here the If statement will evaluate to true if the object o implements the interface IExample or the object is a derived object of IExample.

 

However, we cannot use is operator if all we got is a type and we want to find whether that type implements a particular interface.

 

Type.IsAssignableFrom()

The System.Type class has an IsAssignableFrom() method which does exactly what we want. The IsAssignableFrom() method takes a type as its argument and returns true if the object of the provided type can be assigned to a variable of the current type. See the following example:

 

interface IExample

{

 void Do();

}

class Example : IExample

{

 

    #region IExample Members

 

    public void Do()

    {

        throw new Exception("The method or operation is not implemented.");

    }

 

    #endregion

}

 

Here we are declaring an interface IExample and a class Example which implements the IExample interface. Now let us see how we can check that the type Example implements IExample interface.

 

if(typeof(IExample).IsAssignableFrom(typeof(Example)))

Console.WriteLine("Yes");

 

This will output “yes” to the console as the interface IExample is implemented by class Example class. For more information on Type.IsAssignableFrom() method, visit this MSDN page.

 

Happy coding!

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Basics of PHP

clock December 21, 2008 16:56 by author Nirandas

What is PHP?

PHP is a high level scripting language particularly used in developing websites. PHP is very powerful and easy to learn. PHP is platform independent that is, PHP can run on either Linux or Windows and many other OS. Unlike C or C++, PHP is an interpreted language that is, the PHP code we write doesn’t get converted into machine code. Instead the PHP reads and executes the source code when we run the script. For more information about PHP and its history, visit http://en.wikipedia.org/wiki/PHP

More...

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Who Am I?

I am Nirandas - a developer from INDIA

Sign in