0

I have a method which gets the name of a sub class from an error message, as below

string jobDesc = getDtlName(serviceResponse.ErrorMessages[0].Error);

I have the Class and want to access the sub class properties but how can I do that when I only have the sub class name as a string.

Java have a method which works like this

Class myClass = Class.forName(jobDesc);

How can I do the same in C#?

user616076
  • 3,907
  • 8
  • 38
  • 64

3 Answers3

0

You can get all loaded assemblies using AppDomain.CurrentDomain.GetAssemblies(),
then get all types of these assemblies with .SelectMany(a => a.GetTypes())
then select the one type with the given name with .Single(t => t.Name == jobDesc).

Keep in mind that Single throws an exception if not exactly 1 element matches the condition.
If you have multiple types with the same name (in different namespaces) you should use Where instead.
If there may be no matching classes, use SingleOrDefault, which returns null if there is no matching element.

AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes())
    .Single(t => t.Name == jobDesc)

If the desired class is in the assembly that is currently executed, you can also use

System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
    .Single(t => t.Name == jobDesc)
Domysee
  • 12,718
  • 10
  • 53
  • 84
  • Thank you for your reply but I get an error 'System.Array does not contain a definition for SelectMany' – user616076 Jan 06 '16 at 09:05
  • @user616076 you have to include the namespace `System.Linq` – Domysee Jan 06 '16 at 09:06
  • Ok, when I ran it I get this error 'Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.' Is there something else I'm missing? – user616076 Jan 06 '16 at 09:16
  • @user616076 what do the LoaderExceptions say? – Domysee Jan 06 '16 at 09:17
  • @user616076 I have added another option for you, maybe this works better. – Domysee Jan 06 '16 at 09:20
  • The LoaderException says 'Could not load file or assembly 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.'. Thanks for the other option, I'll try it now. – user616076 Jan 06 '16 at 09:38
  • Hi Domysee, I've just tried it and it throws this error 'Sequence contains no matching element'. – user616076 Jan 06 '16 at 09:44
  • @user616076 then it seems that the type you are loading is not defined in the executing assembly. – Domysee Jan 06 '16 at 09:46
  • @user616076 `GetAssemblies` tries to load all assemblies referenced in the project, also all dependencies. It seems that something references `Microsoft.Practices.ServiceLocation`, but the assembly is not copied to the output directory. – Domysee Jan 06 '16 at 09:47
  • Ok, I'll try and find what is referencing ServiceLocation and copy the assembly. – user616076 Jan 06 '16 at 09:54
  • @user616076 this could happen with other assemblies too. If you know in which assembly the type you want to load is specified, you can select only this one using `GetAssemblies().Where(a => a.FullName = "YourAssemblyName").SelectMany...` – Domysee Jan 06 '16 at 09:57
0

You need to do something like this,

string jobDesc = getDtlName(serviceResponse.ErrorMessages[0].Error);
SomeClass myClass = new SomeClass();
// set some class values,

var classType = typeof(SomeClass );                   
PropertyInfo info = classType.GetProperty(jobDesc);
var propertyValue = info.GetValue(myClass, null);

You might need to do a minor changes as I haven't tested this solution,

fhnaseer
  • 7,159
  • 16
  • 60
  • 112
0

I managed to get a working solution using Properties with the code below

PropertyInfo[] properties = typeof(UIStaticFieldErrors).GetProperties();
foreach (PropertyInfo property in properties)
{
    if (property.ToString() == "Boolean " + jobDesc)
    {
         property.SetValue(errorSF, true);
    }
}

I get the properties of UIStaticFieldErrors in properties and then iterate through until I get a match for jobDesc, then I update that field to true. The variable errorSF is an instance of UIStaticFieldErrors.

user616076
  • 3,907
  • 8
  • 38
  • 64