locked
can I get all public variables in a class? RRS feed

  • Question

  • User-1982615125 posted

    can I get all public variables in a class, and know their type? (string, Boolean, integer, datetime)

    I need to make an object editor

    Saturday, June 16, 2018 4:07 PM

All replies

  • User-369506445 posted

    hi

    yes, you can, the public is the most common access specifier in C#. It can be access from anywhere, that means there is no restriction on accessibility. The scope of the accessibility is inside a class as well as outside. The type or member can be accessed by any other code in the same assembly or another assembly that references it.

    for get type of a variable, you can try below code :

     public class MyClass
            {
                public int Id;
                public string Name="";
            }

    and

     MyClass o = new MyClass();
                var type_Id = o.Id.GetType().Name;
                var type_Name = o.Name.GetType().Name;
    
                Response.Write("Id is type of " + type_Id);
                Response.Write("Name is type of " + type_Name);

    result :

    Id is type of Int32
    
    Name is type of String 
    Sunday, June 17, 2018 5:10 AM
  • User36583972 posted


    Hi fazio,

    can I get all public variables in a class, and know their type? (string, Boolean, integer, datetime)

    It seems you can try to use the C# reflection.

    The classes in the System.Reflection namespace, together with System.Type, enable you to obtain information about loaded assemblies and the types defined within them, such as classes, interfaces, and value types. You can also use reflection to create type instances at run time, and to invoke and access them. For topics about specific aspects of reflection, see Related Topics at the end of this overview.

      Type t = typeof(ChangeValue);
                Type[] types = new Type[0];
                ConstructorInfo ci = t.GetConstructor(types);
                object[] objs = new object[0];
                object obj = ci.Invoke(objs);
                FieldInfo[] fis = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
                Console.WriteLine(fis.Length);
                foreach (var fieldInfo in fis)
                {
                    Response.Write(fieldInfo.FieldType + "  : " + fieldInfo.Name + "   :   " + fieldInfo.GetValue(obj) + "   :   " + fieldInfo.IsPrivate);
                }


    Reflection in the .NET Framework:
    https://docs.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/reflection

    Reflection in C# Tutorial
    https://www.codeproject.com/Articles/17269/Reflection-in-C-Tutorial


    Best Regards,

    Yong Lu

    Monday, June 18, 2018 8:23 AM
  • User-513218303 posted

    Hi,

    Fortunately I had same requirement and I written the piece of code which I modified for you.

    Please use and let me know if it fulfills your requirement.

    Namespace required :

    using System.Reflection;

    Sample class :

            public class TestClass
            {
                public string str { get; set; }
                public int inVal { get; set; }
                public TestClass2 obj22 { get; set; }
            }
    
            public class TestClass2
            {
                public string str22 { get; set; }
                public int inVal22 { get; set; }
            }

    Function to get type of all variables in class :

    public string GET_TYPE_OF_CLASS_MEMBERS(Object objRequest)
            {
                var strtypes = string.Empty;
                try
                {
                    Type type = objRequest.GetType();
                    PropertyInfo[] properties = type.GetProperties();
    
                    foreach (PropertyInfo property in properties)
                    {
                        if (property.PropertyType.IsClass && !property.PropertyType.FullName.StartsWith("System."))
                        {
                            strtypes += GET_TYPE_OF_CLASS_MEMBERS(property.GetValue(objRequest, null));
                        }
                        else
                        {
                            try
                            {
                                strtypes += property.Name + " - " + property.PropertyType.FullName + "\n";
                            }
                            catch (Exception Ex)
                            {
                                strtypes = Ex.Message;
                            }
                        }
                    }
                }
                catch (Exception Ex)
                {
                    strtypes = Ex.Message;
                }
                return strtypes;
            }

    Function call :

                TestClass obj = new TestClass();
                obj.obj22 = new TestClass2();
                string str = GET_TYPE_OF_CLASS_MEMBERS(obj);

    Output :

    str - System.String
    inVal - System.Int32
    str22 - System.String
    inVal22 - System.Int32

    Variables and classes created in hurry so names may look messy...

    Please give it a try and let me know if any assistance required.

    Monday, June 18, 2018 1:15 PM