Hi...............
Ques-: What is reflection in .net with give a example.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Nov 19, 2011, 12:23 AM
Please refer the below link
http://www.codeproject.com/Articles/206412/Reflection-101-An-Introduction-to-Reflection-in-NE
Thanks
Datta KharadPosted Nov 19, 2011, 12:21 AM
Reflection:-
Reflection provides objects (of type Type) that describe assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, reflection enables you to access them.
Reflection is useful in the following situations:
When you have to access attributes in your program's metadata.
#For examining and instantiating types in an assembly.
#For building new types at runtime. Use classes in System.Reflection.Emit.
#For performing late binding, accessing methods on types created at run time. See the topic Dynamically Loading and Using Types.
Reflection is a very powerful feature that any programming language would like to provide, because it allows us to get some information about objects in runtime. It can be used in the applications normally but this is provided for doing some advanced programming. This might be for runtime code generation (It goes through creating, compilation and execution of source code in runtime).
Example:-
using System;
using System.Reflection;
namespace ReflectionQueryTest
{
public class TestBaseClass {}
public class TestDerivedClass : TestBaseClass {}
struct TestStruct {}
interface TestInterface {}
class TestAttribute : System.Attribute {}
enum TestEnum {}
class Class1
{
private static void ReflectType(string sTypeName)
{
try
{
// get the type from the given string
Type type = Type.GetType(sTypeName);
Console.WriteLine("Type name: {0}", type.FullName);
Console.WriteLine("\tHasElementType = {0}",
type.HasElementType);
Console.WriteLine("\tIsAbstract = {0}",
type.IsAbstract);
Console.WriteLine("\tIsAnsiClass = {0}",
type.IsAnsiClass);
Console.WriteLine("\tIsArray = {0}", type.IsArray);
}
catch (System.NullReferenceException)
{
Console.WriteLine("{0} is not a valid type", sTypeName);
}
}
static void Main(string[] args)
{
// Reflect all the attributes for the given type by
// passing the name of the type
ReflectType("System.Int32");
ReflectType("ReflectionQueryTest.TestDerivedClass");
ReflectType("ReflectionQueryTest.TestStruct");
ReflectType("ReflectionQueryTest.TestBaseClass");
ReflectType("ReflectionQueryTest.TestInterface");
ReflectType("ReflectionQueryTest.TestAttribute");
ReflectType("ReflectionQueryTest.TestEnum");
}
}
}
You can also refer this link...
http://www.codeproject.com/KB/cs/C__Reflection_Tutorial.aspx