Click here to Skip to main content
15,895,011 members
Home / Discussions / C#
   

C#

 
AnswerRe: Parsing a Path Pin
Pete O'Hanlon27-Aug-12 23:27
mvePete O'Hanlon27-Aug-12 23:27 
Questionoh yes, using 'dynamic <i>will<i> do something special for you ! Pin
BillWoodruff27-Aug-12 15:33
professionalBillWoodruff27-Aug-12 15:33 
AnswerRe: oh yes, using 'dynamic will do something special for you ! Pin
DaveyM6927-Aug-12 22:34
professionalDaveyM6927-Aug-12 22:34 
GeneralRe: oh yes, using 'dynamic will do something special for you ! Pin
BillWoodruff28-Aug-12 3:34
professionalBillWoodruff28-Aug-12 3:34 
GeneralRe: oh yes, using 'dynamic will do something special for you ! Pin
DaveyM6928-Aug-12 3:50
professionalDaveyM6928-Aug-12 3:50 
GeneralRe: oh yes, using 'dynamic will do something special for you ! Pin
BillWoodruff28-Aug-12 14:58
professionalBillWoodruff28-Aug-12 14:58 
AnswerRe: oh yes, using 'dynamic will do something special for you ! Pin
DaveyM6928-Aug-12 4:15
professionalDaveyM6928-Aug-12 4:15 
GeneralRe: oh yes, using 'dynamic will do something special for you ! Pin
BillWoodruff28-Aug-12 15:09
professionalBillWoodruff28-Aug-12 15:09 
Hi DaveyM69,

Yes, this will work, but note that you are required to know what Type you want returned, and specify it in the call to GetValue<T> : to me that is really the same requirement as having to do an explicit cast on the result of Convert.ChangeType, just more elegant Smile | :) .

Appreciate your comments, but, so far, still convinced that 'dynamic adds something unique to this (albeit esoteric) area of .NET Type conversion we are exploring.

... edit ...

Note that this code, similar to yours in structure, would work without the need for type specification:
XML
public static dynamic x_GetValue<T>(KeyValuePair<Type, object> keyValuePair)
{
    return Convert.ChangeType(keyValuePair.Value, typeof(T));
}
// usage example
string kvpString = x_GetValue<dynamic>(kvpTO);
// an alternate usage using 'var
var kvpString = x_GetValue<dynamic>(kvpTO);

kvpString += "... even more text";

Console.WriteLine(kvpString.GetType() + " : " + kvpString);
However, I think a possible valid "challenge" to using 'dynamic, compared to other strategies that return 'object: is that the programmer is still going to need to know, in all cases, what type of value will be returned here ... unless the programmer implements some kind of switch/case statement to take action based on the run-time type.

I'm trying to imagine a real-world use case where using 'dynamic, in the ways explored on this thread, would be really required, and "best practice:" I admit to failing to do that Smile | :) ... so far.

Would it benefit others on CP, to write up this usage of 'dynamic as a tip-trick ? Not sure !

... end edit ...

best, Bill

"If you shoot at mimes, should you use a silencer ?" Stephen Wright


modified 29-Aug-12 0:21am.

GeneralRe: oh yes, using 'dynamic will do something special for you ! Pin
DaveyM6928-Aug-12 21:24
professionalDaveyM6928-Aug-12 21:24 
QuestionMailMessage syntax with using statement Pin
thewazz27-Aug-12 10:46
professionalthewazz27-Aug-12 10:46 
AnswerRe: MailMessage syntax with using statement Pin
Eddy Vluggen27-Aug-12 11:01
professionalEddy Vluggen27-Aug-12 11:01 
AnswerRe: MailMessage syntax with using statement Pin
Wes Aday27-Aug-12 11:03
professionalWes Aday27-Aug-12 11:03 
GeneralRe: MailMessage syntax with using statement Pin
thewazz27-Aug-12 12:32
professionalthewazz27-Aug-12 12:32 
QuestionPlay Incoming Message Notification Pin
Jassim Rahma27-Aug-12 10:43
Jassim Rahma27-Aug-12 10:43 
AnswerRe: Play Incoming Message Notification Pin
Wes Aday27-Aug-12 10:45
professionalWes Aday27-Aug-12 10:45 
GeneralRe: Play Incoming Message Notification Pin
Jassim Rahma27-Aug-12 10:52
Jassim Rahma27-Aug-12 10:52 
GeneralRe: Play Incoming Message Notification Pin
Wes Aday27-Aug-12 11:00
professionalWes Aday27-Aug-12 11:00 
AnswerRe: Play Incoming Message Notification Pin
Eddy Vluggen27-Aug-12 11:08
professionalEddy Vluggen27-Aug-12 11:08 
GeneralRe: Play Incoming Message Notification Pin
Wes Aday27-Aug-12 11:15
professionalWes Aday27-Aug-12 11:15 
GeneralRe: Play Incoming Message Notification Pin
Eddy Vluggen27-Aug-12 11:22
professionalEddy Vluggen27-Aug-12 11:22 
QuestionSidebar using MDI Child Pin
Jassim Rahma27-Aug-12 10:35
Jassim Rahma27-Aug-12 10:35 
AnswerRe: Sidebar using MDI Child Pin
Eddy Vluggen27-Aug-12 10:58
professionalEddy Vluggen27-Aug-12 10:58 
GeneralRe: Sidebar using MDI Child Pin
Jassim Rahma28-Aug-12 10:40
Jassim Rahma28-Aug-12 10:40 
QuestionBest way to get notification from Sql server ?!? Pin
EgyptianRobot27-Aug-12 9:11
EgyptianRobot27-Aug-12 9:11 
AnswerRe: Best way to get notification from Sql server ?!? Pin
fjdiewornncalwe27-Aug-12 9:17
professionalfjdiewornncalwe27-Aug-12 9:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.