0

i have a class named Hacker and i have a class Spy in witch i have to do a method that takes the name(single name) of Hacker class but ass a string "Hacker" do some work and return it as a string and im suppose to get the job done by this code in my main method:

        Spy spy = new Spy();
        string result = spy.AnalyzeAccessModifiers("Hacker");
        Console.WriteLine(result);

In my AnalyzeAccessModifiers method I tried:

public string AnalyzeAccessModifiers(string className)
    {
     // i need to get some job done with:
     Type type = Type.GetType(classname);//but it need the fully qualified name for that.
     // so i tried:
     Type type = typeof(className); // but it does not work with string...
    }

is there any way this to be done when i only have the name as a string ...the only way i can think off is by using the actual class name as a type not as a string but that is not in the description of my task...

  • there are two things if the AnalyzeAccessModifiers method is in the Spy class then you can get type by this.GetType(); in your AnalyzeAccessModifiers method. but if AnalyzeAccessModifiers is general method then you need to pass two parameters AnalyzeAccessModifiers(Type type, string className) – Muhammad Waqas Aziz Mar 09 '22 at 05:20
  • So you only have the type name, not the namespace? If you know at least the assembly, you could enumerate all types in the assembly and pick the one with this name. But note that there might be mutiple types with the same name but different namespaces. – Klaus Gütter Mar 09 '22 at 05:23

1 Answers1

0

Sadly Type.GetType(string) is quite finicky about the details it requires before it will give you the result. If the type you're looking for is in current executing assembly (or mscorlib/System.Private.CoreLib) then a fully specified name (including namespace) will get the result. If it's in a different assembly then you need to specify the assembly name.

Type type = null;

// Fails
type = Type.GetType("String");

// Succeeds from core library
type = Type.GetType("System.String");

// Fails (loading from a different assembly)
type = Type.GetType("System.Text.Json.JsonSerializer");

// Succeeds (.NET 6.0)
type = Type.GetType("System.Text.Json.JsonSerializer, System.Text.Json");

Which is all well and good until you're trying to find a type that you have exactly one piece of information about: the class name. Without namespace and assembly name you're stuck with enumerating every type until you find what you're looking for...

static Type? TypeFromName(string name) =>
    // All loaded assemblies
    AppDomain.CurrentDomain.GetAssemblies()
    // All types in loaded assemblies
    .SelectMany(asm => asm.GetTypes())
    // Return first match
    .FirstOrDefault(type => type.Name == name);

Simple, yes. Fast? Not even remotely. But you only need to do the search once and cache the results for later use.

Corey
  • 15,524
  • 2
  • 35
  • 68
  • It turns out that they have a mistake in the task(this is from my University) they should have give us the fully qualified name. – mr.Dimitrov Mar 09 '22 at 06:44
  • Still handy to know how to iterate/filter the list of types. I use this kind of method to find classes in loaded assemblies that implement specific interfaces. – Corey Mar 09 '22 at 08:56
  • hey i tried your suggestion but i cant figure out that mistake Im getting in .SelectMany `var test = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(asm => asm.GetTypes() .FirstOrDefault(type => type.Name == className));` [link] https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0411?f1url=%3FappId%3Droslyn%26k%3Dk(CS0411) I finally did it like that `Type classType = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(x => x.Name == className);` – mr.Dimitrov Mar 09 '22 at 11:02
  • @mr.Dimitrov Sorry, missed a `)` while typing the code. – Corey Mar 09 '22 at 22:54
  • thats not a issue but i still don't know what that exception mean when i use the code like this `var test = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(asm => asm.GetTypes() .FirstOrDefault(type => type.Name == className));` – mr.Dimitrov Mar 10 '22 at 05:52