|
-
Sep 13th, 2007, 10:57 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Late Binding - Casting
Hi folks, I recently moved from vb.net to c#. I normally store my dataview in a session object for sorting and paging purposes. (asp.net apps)
I'm curious to know what is the difference between the following....
Code:
Type T = Session["SessionDataViewTable"].GetType();
/*Above, I get the type of the Session variable that contains
a dataview */
object[] param = new object[1];// creating an object array with only 1 element in size
param[0] = e.SortExpression + " " + Session["SortOrder"];
//Above, I set the element zero to the initial sort order string
T.InvokeMember("Sort", BindingFlags.SetProperty, null, Session["SessionDataViewTable"], param);
//Above, I Invoke the "sort" member of the dataview object.
/* I had to check before what type of member it is, and that is
why I specify in the binding flags that it is a SetProperty */
and....
Code:
((DataView)Session["SessionDataViewTable"]).Sort = "id" + " " + Session["SortOrder"];
Originally, when I first moved from vb.net to c#(last week), I tried to do this...
Code:
Session["SessionDataViewTable"].Sort = "id" + " " + Session["SortOrder"];
I got a compiler error stating something to do with Sort not being a member of that session variable. So I did the research and found out that vb.net allows this late binding that I got used to. I read online that I need to do my own late binding. Then I find out that I could just cast it to get the method.
Is there a difference beside the fact that one of my options requires only 1 line of code?
Thx
-
Sep 14th, 2007, 09:10 AM
#2
Re: Late Binding - Casting
The first method uses reflection and is thus horribly inefficient and nearly unreadable. The second method should always be preferred. Ditto in VB.NET; you should enable Option Strict, which disallows late binding without explicit casting.
Unless you have a very good reason to use reflection, don't.
As well as the parenthesised (cast) operator, you can also use the 'as' operator. The difference between these is that whilst (T)O will throw an 'invalid cast' exception if O cannot be cast to T, (O as T) will return null. As well as that point I often use the the latter simply because I find it more readable.
-
Sep 14th, 2007, 10:45 AM
#3
Thread Starter
Addicted Member
Re: Late Binding - Casting
-
Sep 26th, 2007, 06:41 AM
#4
Re: [RESOLVED] Late Binding - Casting
One important difference is that (O as T) will not use any custom-written conversion code you may have implemented, whereas (T) will...
//code culled and butchered from MSDN...
class ClassT
{
public static explicit operator ClassT(int ClassO)
{
ClassT temp = new ClassT();
//....blah....
return temp;
}
}
class ClassO
{
public static implicit operator int(ClassO m)
{
//....blah....
}
}
I don't live here any more.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|