2

I have a set of strings like this:

System.Int32
string
bool[]
List<MyType.MyNestedType>
Dictionary<MyType.MyEnum, List<object>>

I would like to test if those strings are actually source code representations of valid types.

I'm in an environment, that doesn't support Roslyn and incorporating any sort of parser would be difficult. This is why I've tried using System.Type.GetType(string) to figure this out.

However, I'm going down a dirty road, because there are so many edge cases, where I need to modify the input string to represent an AssemblyQualifiedString. E.g. nested type "MyType.MyNestedType" needs to be "MyType+MyNestedType" and generics also have to be figured out the hard way.

Is there any helper method which does this kind of checking in .Net 2.0? I'm working in the Unity game engine, and we don't have any means to switch our system to a more sophisticated environment with available parsers.

Clarification My company has developed a code generation system in Unity, which is not easily changed at this point. The one thing I need to add to it, is the ability to get a list of fields defined in a class (via reflection) and then separate them based on whether they are part of the default runtime assembly or if they are enclosed within #if UNITY_EDITOR preprocessor directives. When those are set, I basically want to handle those fields differently, but reflection alone can't tell me. Therefore I have decided to open my script files, look through the text for such define regions and then check if a field is declared within in them, and if true, put it in a separate FieldInfo[] array.

The one thing fixed and not changeable: All script will be inspected via reflection and a collection of FieldInfo is used to generate new source code elsewhere. I just need to separate that collection into individual ones for runtime vs editor assembly.

Xarbrough
  • 1,393
  • 13
  • 23

1 Answers1

1

Custom types and nested generics are probably the hard part.

Can't you just have a "equivalency map to fully qualified name" or a few translation rules for all custom types ? I guess you know by advance what you will encounter.

Or maybe run it on opposite way : at startup, scan your assembly(s) and for each class contained inside, generates the equivalent name "as it's supposed to appear" in your input file from the fully qualified name in GetType() format ?

For custom types of other assemblies, please note that you have to do things such as calling Assembly.LoadFile() or pass assembly name in second parameter to GetType() before to be able to load them. See here for example : Resolve Type from Class Name in a Different Assembly

Maybe this answer could also help : How to parse C# generic type names?

Could you please detail what is the final purpose of project ? The problem is a bit surprising, especially for a unity project. Is it because you used some kind of weird serialization to persist state of some of your objects ?

This answer is more a few recommandations and questions to help you to clarify the needs than a definitive answer, but it can't hold in a single comment, and I think it provide useful informations

Community
  • 1
  • 1
AFract
  • 8,868
  • 6
  • 48
  • 70
  • I don't thing your suggestion is realistic, you can clearly see from the example that generics are an option, which makes a near infinite amount of types to catalog. – Matan Shahar Feb 02 '17 at 16:28
  • @ISun > but a lot of them are completely useless and meaningless, usually you don't use a lot of nested generics and even if it would be the case, most of the trivial can be parsed easily (dictionary for example, you just have to intelligently split the string parts to extract X et Y names, etc!). The initial need is kinda weird, so the solution won't probably be as straightforward as "Call Type.GetType()" ;) – AFract Feb 02 '17 at 16:31
  • You are completely right in that most permutations of generic types are useless. But how do you decide which are useful and which aren't? Can you ensure that types you deemed useless will not ever be requested? – Matan Shahar Feb 02 '17 at 16:38
  • @ISun this is why I also asked clarifications about the needs. OP told he works with Unity, a game/serious game multi purpose engine. He's probably not writing a code compiler, he probably works on a "finite" set of cases and situations. It's to him to give us details ! And my remarks on "parsing" names with generics is still relevant as long as they are expressed in non ambiguous way. One of the link of my answer illustrates this. – AFract Feb 02 '17 at 16:44
  • Thanks for your input so far. I've added some more info to my past. Basically, I only want to know, if a field in a list of FieldInfo (via reflection) is defined within a preprocessor directive like #if UNITY_EDITOR. I haven't found any API way of querying this, so I decided to "parse" through my source files, looking for member declarations and later checking if they are within a define. But so far it's getting real' ugly. – Xarbrough Feb 02 '17 at 17:08