-1

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

uzay95
  • 16,052
  • 31
  • 116
  • 182
  • 1
    That picture is unreadable, and the description isn't terribly easy to understand either. Could you give a short but complete program which shows what you're trying to do? – Jon Skeet Jan 26 '12 at 16:01
  • while you're at it, perhaps rename `ameliyatGirisBilgileri' to something else as well – Adrian Jan 26 '12 at 16:03
  • If it's a local variable then reflection will be useless, do you mean a class field or property? – vc 74 Jan 26 '12 at 16:06
  • See [http://stackoverflow.com/questions/930147/c-sharp-get-type-of-null-object][1], check herzmeister's answer 3rd down. [1]: http://stackoverflow.com/questions/930147/c-sharp-get-type-of-null-object – Giuseppe Romagnuolo Jan 26 '12 at 16:07
  • I'm gonna put better pictures. @vc74, yes I'm looking for type of property of `giris` variable. – uzay95 Jan 26 '12 at 16:08

2 Answers2

1

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.

Community
  • 1
  • 1
Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
1
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.

vc 74
  • 37,131
  • 7
  • 73
  • 89