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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Datta KharadRank #2,135 · 668 reputationPosted Nov 18, 2011, 7:21 PM EST
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
Satyapriya NayakRank #47 · 31.5K reputationPosted Nov 18, 2011, 7:23 PM EST
Please refer the below link
http://www.codeproject.com/Articles/206412/Reflection-101-An-Introduction-to-Reflection-in-NE
Thanks