help me fix the error, error: cannot convert string to system.type

// Convenience utilities written by Malte Hildingsson, malte@hapki.se.
// No copyright is claimed, and you may use it for any purpose you like.
// No warranty for any purpose is expressed or implied.

using System;
using UnityEngine;
using UnityEditor;

namespace Hapki {
public static class EnumExtensions {
    public static T[] GetValuesAsInstances<T>(Type etype) {
        var names = Enum.GetNames(etype); //aaaa
        var array = (T[]) Array.CreateInstance(typeof(T), names.Length);
        var assembly = etype.Assembly.FullName;
        var space = etype.Namespace;
        for (int i = 0, n = names.Length; i < n; ++i) {
            var fullName = space != null ? space + "." + names[i] : names[i];
            array[i] = (T) Activator.CreateInstance(assembly, fullName).Unwrap();
        }
        return array;
    }

    public static T Parse<T>(string name)
            where T : struct, IConvertible {
#if NET_4_6
        T e;
        Enum.TryParse<T>(name, out e);
        return e;
#else
        try {
            return (T) Enum.Parse(typeof(T), name);
        } catch {
            return default(T);
        }
#endif
    }
}

public class EnumFlagsAttribute : PropertyAttribute {
}

public class SerializedEnumAttribute : PropertyAttribute {
    public readonly Type etype;

    public SerializedEnumAttribute(Type etype) {
        this.etype = etype;
    }
}

}

the script is from book of dead project script name: Hapki.EnumExtensions.cs
error message: Assets\Code\ExternalContent\HapkiLibs\Source\Hapki.EnumExtensions.cs(19,53): error CS1503: Argument 1: cannot convert from ‘string’ to ‘System.Type’

Use code tags, explain exactly where the error occurs.

the error is of NET_4_6

Please edit your post and put your code inside code tags, you can find out how here: Using code tags properly
And then please show us the whole error message, which line it is pointing to?

I’m guessing without reading your text-salad that you’re trying to return a string instead of T, but I’m not sure, when you have done both things we will see what’s the problem.

When asking for help with an error, you should always clearly indicate the exact line of code that has the error. (The line number from the error message is sufficient if (but only if) you post your code in such a way that the line numbers match, which doesn’t appear to be the case here.)

Thinking your error is probably on line 23 of your posted code, where you have

Enum.TryParse(name, out e);

which is invalid. There’s several different variations of Enum.TryParse, not completely sure which one you were aiming for, my best guess is

Enum.TryParse<T>(name, out e);

now i have uploaded whole code sorry for inconvenience. it is still showing same error

Open Project Setting → Player, Change your “Api Compatibility Level” to .Net 4.x

7 Likes

I have fixed it! Thank you !