Which is better
Reply
![]() |
|
|
From:
Anand Kumar
|
Hi Group,
Here is my thought
Reflection is a mechanism helps to check and compare types, enumerate methods and fields and finally create and execute types at runtime. However reflection is expensive process because dynamic creation and execute types at runtime ,compare types and method or fields involves lot of expensive tasks and many reflection APIs need to search and parse the metadata. This type of tasks degrade the performance of your application.
The late binding technique uses reflection internally and is an expensive operation that should be avoided in performance critical code.
So you should consider the below points helps to minimize the performance impact of reflection or late binding.
* Visual Basic .NET uses reflection implicitly when you declare the type as object. In C#, you use reflection explicitly. You should avoid reflection wherever possible by using early binding and declaring types explicitly.You should use reflection when you need to perform type comparisons using TypeOf, GetType, and IsInstanceOfType or late bound enumeration using Type.GetFields or late bound execution using Type.InvokeMember
* Early binding allows the compiler to identify the specific type required and perform optimizations that are used at run time. Late binding defers the type identification process until run time and requires extra processing instructions to allow type identification and initialization.try to avoid late binding.Look at the below code.
Assembly asm = System.Reflection.Assembly.LoadFrom("C:\\myAssembly.dll");
Type myType = asm.GetType("myAssembly.MyTypeName");
Object myinstance = Activator.CreateInstance(myType);
This is the equivalent of the following
MyTypeName myinstance = new MyTypeName();
* Try to avoid System.Object This data type can represent any value type or reference type but requried late bound calls to execute method or property.The VB.NET compiler implicitly uses reflection if you declare the type as Object.
Dim obj As Object Set Obj = new CustomType() Obj.CallSomeMethod()
Note: This is a Visual Basic .NET specific issue. C# has no such problem
* By default, Visual Basic .NET allows late bound code. Set the Strict and Explicit properties to true to force Visual Basic .NET to not allow late bound code. In Visual Studio .NET, you can access these properties through the Project Properties dialog box.
* When you use P/Invoke or COM interop, the interop code is subject to permission demands that walk the call stack to ensure that the calling code is authorized to call unmanaged code.You can use the SuppressUnmanagedCodeSecurity attribute to improve performance by eliminating the additional security check operations.
For more information refer http://msdn.microsoft.com/library/en-us/dv_vstechart/html/vbtchPerfOpt.asp
http://msdn.microsoft.com/library/en-us/dndotnet/html/highperfmanagedappsasp
Cheers
Anand
http://spaces.msn.com/members/anandkumar |
|
View other groups in this category.
![]() |
To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings.
Need help? If you've forgotten your password, please go to Passport Member Services.
For other questions or feedback, go to our Contact Us page.
If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list.
Remove my e-mail address from dotNET User Group Hyd.
|
|