Click here to Skip to main content
15,914,795 members
Home / Discussions / C#
   

C#

 
GeneralRe: Can't get 24 bit audio to play with directsound Pin
kshet2617-Jan-08 9:19
kshet2617-Jan-08 9:19 
GeneralRe: Can't get 24 bit audio to play with directsound Pin
martin_hughes17-Jan-08 9:27
martin_hughes17-Jan-08 9:27 
GeneralRe: Can't get 24 bit audio to play with directsound Pin
kshet2617-Jan-08 12:06
kshet2617-Jan-08 12:06 
GeneralRe: Can't get 24 bit audio to play with directsound Pin
Jammer31-Mar-08 10:53
Jammer31-Mar-08 10:53 
Generalvisual Studio 2005 class diagram Pin
netJP12L17-Jan-08 6:28
netJP12L17-Jan-08 6:28 
GeneralRe: visual Studio 2005 class diagram Pin
ChrisKo17-Jan-08 8:02
ChrisKo17-Jan-08 8:02 
GeneralRe: visual Studio 2005 class diagram Pin
netJP12L17-Jan-08 9:29
netJP12L17-Jan-08 9:29 
QuestionCreateDelegate with a static Dictionary<string, delegate="">, followed by DynamicInvoke</string,> Pin
Leonardo Pelisoli17-Jan-08 3:54
Leonardo Pelisoli17-Jan-08 3:54 
Hello,

I'm trying to eliminate a huge switch() block in my code by the use of Reflection. I added a dictionary to the class, and then tried populating it in a static constructor. The goal is that later I'll simply get delegates from this dictionary and use DynamicInvoke on them, which would replace the switch.

An example of what I'm trying to do:
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ClassA a = new ClassA();
        }
    }

    class ClassA
    {
        static ClassA()
        {
            foreach (MethodInfo mi in typeof(ClassA).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic))
                if (mi.ReturnType == typeof(UInt16))
                {
                    ParameterInfo[] pis = mi.GetParameters();
                    if ((pis[0].ParameterType == typeof(String).MakeArrayType()) &&
                        (pis[1].ParameterType == typeof(Byte).MakeArrayType()) &&
                        (pis[2].ParameterType == typeof(Byte).MakeArrayType().MakeByRefType()) &&
                        (pis[2].IsOut))
                    {
                        CallbackMethods.Add(mi.Name, Delegate.CreateDelegate(typeof(CallbackMethodDelegate), mi));
                    }
                }
        }

        private static Dictionary<string, delegate=""> CallbackMethods = new Dictionary<string, delegate="">();

        private delegate UInt16 CallbackMethodDelegate(String[] header, Byte[] mgedDataIn, out Byte[] mgedDataOut);

        private UInt16 Method1(String[] header, Byte[] mgedDataIn, out Byte[] mgedDataOut)
        {
            mgedDataOut = null;
            return (UInt16)0;
        }
    }
}</string,></string,>

Executing this code gives me a TypeInitializationException when running the Delegate.CreateDelegate() method.

This is undoubtedly linked to the fact that I want to invoke instance methods, while the delegates are being created in a static constructor. In fact, if I copy and paste the code of my static constructor into an instance constructor, make the dictionary an instance variable, and call a different overload of CreateDelegate (the one that receives firstArgument, since I now have this) it works.

So my question would be: is there a way to create these delegates in the static constructor, and then later when calling DynamicInvoke on them, bind them to the correct instance of the class?

Thanks in advance for any suggestions you can offer.
Leonardo Pelisoli
GeneralRe: CreateDelegate with a static Dictionary, followed by DynamicInvoke Pin
Skippums17-Jan-08 5:30
Skippums17-Jan-08 5:30 
GeneralRe: CreateDelegate with a static Dictionary, followed by DynamicInvoke Pin
Leonardo Pelisoli19-Jan-08 12:37
Leonardo Pelisoli19-Jan-08 12:37 
GeneralRe: CreateDelegate with a static Dictionary, followed by DynamicInvoke Pin
Skippums21-Jan-08 4:44
Skippums21-Jan-08 4:44 
GeneralRe: CreateDelegate with a static Dictionary, followed by DynamicInvoke Pin
Leonardo Pelisoli27-Jan-08 7:15
Leonardo Pelisoli27-Jan-08 7:15 
GeneralComboBox Scrollwheel disable Pin
R.Myers17-Jan-08 3:41
R.Myers17-Jan-08 3:41 
GeneralRe: ComboBox Scrollwheel disable Pin
DaveyM6917-Jan-08 4:27
professionalDaveyM6917-Jan-08 4:27 
GeneralRe: ComboBox Scrollwheel disable Pin
DaveyM6917-Jan-08 5:00
professionalDaveyM6917-Jan-08 5:00 
GeneralRe: ComboBox Scrollwheel disable Pin
R.Myers17-Jan-08 9:09
R.Myers17-Jan-08 9:09 
Generalrtb / rich text box Pin
Member 394658217-Jan-08 3:40
Member 394658217-Jan-08 3:40 
GeneralRe: rtb / rich text box Pin
Luc Pattyn17-Jan-08 3:55
sitebuilderLuc Pattyn17-Jan-08 3:55 
Generaldisplay image located on client Pin
Chazzysb17-Jan-08 3:34
Chazzysb17-Jan-08 3:34 
GeneralCrystal report stored procedure server location Pin
Scalee17-Jan-08 2:54
Scalee17-Jan-08 2:54 
GeneralRe: Crystal report stored procedure server location Pin
Chazzysb17-Jan-08 3:44
Chazzysb17-Jan-08 3:44 
GeneralChange Components icon Pin
Abhijit Jana17-Jan-08 2:32
professionalAbhijit Jana17-Jan-08 2:32 
QuestionRe: Change Components icon Pin
TJoe17-Jan-08 4:03
TJoe17-Jan-08 4:03 
QuestionWPF - Which Components? Pin
Tomerland17-Jan-08 2:31
Tomerland17-Jan-08 2:31 
AnswerRe: WPF - Which Components? Pin
Philipp Sumi17-Jan-08 2:40
Philipp Sumi17-Jan-08 2:40 

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.