Search code examples
c#visual-studiomodel-view-controlleredmxtypeof

How can I convert my file name as a string variable to fit with typeof(filename)?


I'm using a list of file names to produce a break down of the files structure. Using the file name I can get the list of field names using

var FieldNames = typeof(Carers).GetProperties().Select(f => f.Name).ToList();

However, if I replace 'Carers' with is obviously a Class with the string variable of the Class name it causes conflict with typeof(). How can I convert the string variable to work with typeof()?


Solution

  • You cannot use typeof("type_name") to get a Type. Instead you should use either:

    Type.GetType("type_name").GetProper...
    

    or

    Assembly.GetType("type_name").GetProper...
    

    In the first method, you may need to specify which Assembly the type belongs to (ref: Type.GetType Method)

    In the second method you would need to fetch whichever Assembly defined the type (ref: Assembly.GetType Method)

    In either case, they return a Type which you can use GetProperties on as you are in your existing code.