Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Using MakeGenericMethod #5939

Closed
FoggyFinder opened this issue Jun 14, 2018 · 2 comments
Closed

Using MakeGenericMethod #5939

FoggyFinder opened this issue Jun 14, 2018 · 2 comments

Comments

@FoggyFinder
Copy link

I tried to compile simple example with using of MakeGenericMethod:

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\r\n--- Examine a generic method.");

            Type ex = typeof(Example);
            MethodInfo mi = ex.GetMethod("Generic");

            MethodInfo miConstructed = mi.MakeGenericMethod(typeof(int));
            object[] oargs = { 42 };
            miConstructed.Invoke(null, oargs);

            MethodInfo miDef = miConstructed.GetGenericMethodDefinition();
            Console.WriteLine("\r\nThe definition is the same: {0}",
                miDef == mi);
        }

    }

    public static class Example
    {
        public static void Generic<T>(T toDisplay)
        {
            Console.WriteLine("\r\nHere it is: {0}", toDisplay);
        }
    }

    public static class Example
    {
        public static void Generic<T>(T toDisplay)
        {
            Console.WriteLine("\r\nHere it is: {0}", toDisplay);
        }
    }
}

but I get the exception below:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at HelloWorld.Program.Main(String[]) + 0xa7
   at HelloWorld!<BaseAddress>+0x1557ee

Here I had read about RD.xml, so I added the next one:

        <Assembly Name="HelloWorld" Dynamic="Required All">
            <Type Name="HelloWorld.Example" Dynamic="Required All"></Type>       
        </Assembly>

but the result was the same

HelloWorld.zip

@MichalStrehovsky
Copy link
Member

You need to specify the exact instantiation you want to use the generic method with in the RD.XML.

The syntax is "documented" here: #4775.

Also here's some details I posted about CoreRT's restrictions around MakeGenericMethod/Type you might find useful: dotnet/fsharp#4954 (comment)

@FoggyFinder
Copy link
Author

FoggyFinder commented Jun 14, 2018

@MichalStrehovsky thank you, I changed my RD to the next one:

        <Assembly Name="HelloWorld" Dynamic="Required All">
            <Type Name="HelloWorld.Example">
                <Method Name="Generic" Dynamic="Required">
                    <GenericArgument Name="System.Int32, System.Private.CoreLib" />
                </Method>
            </Type> 
        </Assembly>

and seems it works 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants