Dotnet-api-docs: This example doesn't work.

Created on 19 Jan 2019  Â·  5Comments  Â·  Source: dotnet/dotnet-api-docs

In the example below, modified for prototyping usage in the real world, t will be null.

        Assembly assem = typeof(Test).Assembly;

        Assembly asm = Assembly.ReflectionOnlyLoad(assem.GetName().Name);
        Type t = asm.GetType("Test");
        MethodInfo m = t.GetMethod("TestMethod");
        ParameterInfo[] p = m.GetParameters();

The modification was made because I have no clue EXACTLY what the hell "Source" is and had to guess.

If the guess is wrong, the example is still horsesh!t because, once again, the crappy teachers writing this crap assume we know what they know.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri2 forum

Most helpful comment

Thanks for your feedback @LJ9999. I'll have to take a look at what's going on.

However, I'd also advise you to take a look at our code of conduct: https://dotnetfoundation.org/code-of-conduct

All 5 comments

Thanks for your feedback @LJ9999. I'll have to take a look at what's going on.

However, I'd also advise you to take a look at our code of conduct: https://dotnetfoundation.org/code-of-conduct

I didn't know there was a code of conduct. I've been fighting Microsoft technologies for 30 years and this is the first time I was aware of such a thing. I guess people are reading the feedback. When did that start?

All I wanted was a moment of hopeless catharsis in a futile last gasp of dying effort to get something to work that is explained so poorly. If you wrote this doc then for sure you have every right to tell me to go to hell. I'm okay with that. I'm a fair man.

Now that I have read the code of conduct combined with the realization that someone reads the feedback (I'm still in shock) I will endeavor to express all future angst with a greater degree of measure.

Hi @LJ9999. The Assembly.GetType method expects the "full name of the type" meaning namespace and name, so your example will only work if the class Test is not inside a namespace. Otherwise, you should also supply the namespace. The following should work.

using System;
using System.Reflection;

namespace TestReflection
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly assem = typeof(Program).Assembly;

            Assembly asm = Assembly.ReflectionOnlyLoad(assem.GetName().Name);
            Type t = asm.GetType("TestReflection.Program");
            MethodInfo m = t.GetMethod("TestMethod");
            ParameterInfo[] p = m.GetParameters();
...
        }

        public void TestMethod(string input) { }
    }
}

@LJ9999 in fact you don't know what you do...

Thanks @mikkelbu for jumping in to help. @LJ9999 please let us know if that solves your issue. For now, I'm closing this.

Was this page helpful?
0 / 5 - 0 ratings