Question

There is main a variable which has uninitialized variables. I need to retrieve the Type of uninitialized varible type with reflection. Because I am generating the values dynamically but can't get the types of main variable's type of vars.

In the picture Quick Watch is showing the type name of ameliyatGirisBilgileri variable even it is not initialized.

as shown in Type field of QuickWatch

Was it helpful?

Solution

FieldInfo fieldInfo = typeof(MyClass).GetField("ameliyatGirisBilgileri", BindingFlags.Public | BindingFlags.Instance);
Type fieldType = fieldInfo.FieldType;

Sorry but I'm too lazy to type the name of your class completely.

OTHER TIPS

You should be able to get the FieldInfo for the variables within the type using the GetField(...) or GetFields(...) method on the main type. Below is a short program demonstrating how you might go about it:

class Program
{
    public string mStringType = null;

    static void Main(string[] args)
    {
        var program = new Program();

        try
        {
            var field = program.GetType().GetField("mStringType");

            Console.WriteLine("Field '{0}' is of type '{1}' and has value '{2}'.", field.Name, field.FieldType.FullName, field.GetValue(program));

            program.mStringType = "Some Value";

            Console.WriteLine("Field '{0}' is of type '{1}' and has value '{2}'.", field.Name, field.FieldType.FullName, field.GetValue(program));
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("Error");
        }

        Console.ReadKey();
    }
}

This gives the following output on the Console window:

Field 'mStringType' is of type 'System.String' and has value ''.

Field 'mStringType' is of type 'System.String' and has value 'Some Value'.

Note: If the fields are not public, you will have to pass some BindingFlags into the GetField(...) or GetFields(...) methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top