Click here to Skip to main content
15,892,517 members
Home / Discussions / C#
   

C#

 
GeneralRe: Unboxing an unknown type Pin
DaveyM6930-Apr-09 1:42
professionalDaveyM6930-Apr-09 1:42 
GeneralRe: Unboxing an unknown type Pin
musefan30-Apr-09 2:51
musefan30-Apr-09 2:51 
AnswerRe: Unboxing an unknown type Pin
DaveyM6930-Apr-09 1:41
professionalDaveyM6930-Apr-09 1:41 
GeneralRe: Unboxing an unknown type Pin
mtherien30-Apr-09 3:02
mtherien30-Apr-09 3:02 
AnswerRe: Unboxing an unknown type Pin
Alan N30-Apr-09 1:59
Alan N30-Apr-09 1:59 
AnswerRe: Unboxing an unknown type Pin
Gideon Engelberth30-Apr-09 2:47
Gideon Engelberth30-Apr-09 2:47 
GeneralRe: Unboxing an unknown type Pin
mtherien30-Apr-09 3:13
mtherien30-Apr-09 3:13 
AnswerRe: Unboxing an unknown type Pin
mtherien30-Apr-09 3:49
mtherien30-Apr-09 3:49 
Using the code in that article, I came up with a modified version fo GetMethod and DynamicCast:
public static MethodInfo GetMethod(Type toSearch, string methodName, Type inputType, BindingFlags bindingFlags)
{
    return Array.Find(toSearch.GetMethods(bindingFlags),
        delegate(System.Reflection.MethodInfo inf) {
            return ((inf.Name == methodName) && (inf.ReturnType == toSearch) && inf.GetParameters().Length == 1 && inf.GetParameters()[0].ParameterType == inputType); });
}

public static T DynamicCast<T>(object o)
{
    Type objectType = o.GetType();
    Type returnType = typeof(T);

    MethodInfo meth = GetMethod(returnType, "op_Implicit", objectType, BindingFlags.Static | BindingFlags.Public);
    if (meth == null)
    {
        meth = GetMethod(returnType, "op_Explicit", objectType, BindingFlags.Static | BindingFlags.Public);
    }
    if (meth == null)
        throw new InvalidCastException("Invalid Cast.");
    return (T)meth.Invoke(null, new object[] { o });
}


This seems to work, may need a little more testing. I'm not sure how this will affect performance.
GeneralRe: Unboxing an unknown type Pin
S. Senthil Kumar30-Apr-09 9:47
S. Senthil Kumar30-Apr-09 9:47 
QuestionHow to write service in C# ? Pin
Yanshof30-Apr-09 0:41
Yanshof30-Apr-09 0:41 
AnswerRe: How to write service in C# ? Pin
stancrm30-Apr-09 0:53
stancrm30-Apr-09 0:53 
AnswerRe: How to write service in C# ? Pin
Alaa' Al Atrash30-Apr-09 2:18
Alaa' Al Atrash30-Apr-09 2:18 
QuestionProblrm with setup project. Pin
Narendra Reddy Vajrala30-Apr-09 0:30
Narendra Reddy Vajrala30-Apr-09 0:30 
AnswerRe: Problrm with setup project. [CROSS-POST Please Ignore!!!!!] Pin
Henry Minute30-Apr-09 0:53
Henry Minute30-Apr-09 0:53 
GeneralRe: Problrm with setup project. [CROSS-POST Please Ignore!!!!!] Pin
Narendra Reddy Vajrala30-Apr-09 0:59
Narendra Reddy Vajrala30-Apr-09 0:59 
QuestionAdding a text file to a c# project Pin
doubleteam30-Apr-09 0:21
doubleteam30-Apr-09 0:21 
AnswerRe: Adding a text file to a c# project Pin
Mycroft Holmes30-Apr-09 0:31
professionalMycroft Holmes30-Apr-09 0:31 
AnswerRe: Adding a text file to a c# project Pin
ATCsharp30-Apr-09 2:40
ATCsharp30-Apr-09 2:40 
Questionan exaple of converting string to code Pin
rachelicohen30-Apr-09 0:09
rachelicohen30-Apr-09 0:09 
AnswerRe: an exaple of converting string to code Pin
saanj30-Apr-09 0:14
saanj30-Apr-09 0:14 
AnswerRe: an exaple of converting string to code Pin
Mycroft Holmes30-Apr-09 0:27
professionalMycroft Holmes30-Apr-09 0:27 
GeneralRe: an exaple of converting string to code Pin
Henry Minute30-Apr-09 0:33
Henry Minute30-Apr-09 0:33 
AnswerRe: an exaple of converting string to code Pin
Ashfield30-Apr-09 1:42
Ashfield30-Apr-09 1:42 
Questionimprove speed of DataGridView CellFormatting event Pin
justeena.paul29-Apr-09 23:23
justeena.paul29-Apr-09 23:23 
AnswerRe: improve speed of DataGridView CellFormatting event Pin
Mycroft Holmes30-Apr-09 0:25
professionalMycroft Holmes30-Apr-09 0:25 

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.