-
June 17th, 2014, 05:59 AM
#1
[RESOLVED] how to reflect a variable name itself into a string
ok so i was trying to get basically all the variables of a class into
into a list or array of strings thru reflect then do more stuff
but im stuck there, trying to get the name of a class feild into a string s
for example
Code:
public class A
{
int someint = 0;
public A(){}
public void void aaaahhhHH()
{
var t = new A();
string s = ( A.someint ?? reflect name stuck; )
ToConsole<A>(t,s);
}
public static void ToConsole<T>(T t, string s)
{
Type myTypeA = typeof(T);
FieldInfo myFieldInfo = myTypeA.GetField(s);
Console.WriteLine("The value of the public field {0} is: '{1}'",s ,myFieldInfo.GetValue(t));
}
}
i know i could just type the name in but
id like to use this on other class's and experiment with it a bit more
Last edited by willmotil; June 17th, 2014 at 06:16 AM.
-
June 17th, 2014, 04:58 PM
#2
Re: how to reflect a variable name itself into a string
Use reflection
To retrieve property names
http://www.csharp-examples.net/refle...roperty-names/
Query for "use reflection to get properties names and values" to find out how to get and set values.
-
June 18th, 2014, 01:56 AM
#3
Re: how to reflect a variable name itself into a string
Thanks for the link ArJay i used the activator create object to get a free method
though i don't know how yet to check , if there is actually a public constructor on the type
i was stuck for a minute on, getting the default value's for public field's that was set to the class from just the type
(correct me if im wrong) i came to the conclusion you cant do it from just reflecting on the type
the desired output i got at the bottom it doesn't include the properties or methods yet ect...
and
here's my console program in case anyone else found it interesting.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace ReflectionConsoleTest
{
class Program
{
static string dline = "_________________________________________________";
static string nline = Environment.NewLine;
// some class ill test against
public class SomeClass
{
public static string Name = "bob";
public string Description = "Not much to describe maybe";
public static int Height = 100;
private static int Width = 50;
private int Weight = 300;
}
// entry point
static void Main(string[] args)
{
Console.WriteLine(dline + nline+"Get info of the class type" +nline + dline);
// reflect
ReflectionsInfo.GetInfoForClassType(typeof(SomeClass)); // reflect
Console.WriteLine(dline + nline + "press a key to continue");
Console.ReadLine();
Console.WriteLine(dline + nline + "Get info of a instatiatable class itself via passing a constructable type" + nline + dline);
// reflect
ReflectionsInfo.GetInfoForClassTypeViaConstruction(typeof(SomeClass));
Console.WriteLine(dline + nline + "press a key to continue");
Console.ReadLine();
SomeClass someclassobjinst = new SomeClass();
SomeClass.Name = "Fred";
someclassobjinst.Description = "Something to describe now";
Console.WriteLine(dline + nline + "Get info of a object instantiation of the class" + nline + dline);
// reflect
ReflectionsInfo.GetInfoForClassObjectInstance<SomeClass>(someclassobjinst);
Console.WriteLine(dline+ nline + "press a key to exit");
Console.ReadLine();
}
// the utility class
public static class ReflectionsInfo
{
public static void GetInfoForClassTypeViaConstruction(Type T)
{
Object t = Activator.CreateInstance(T);
GetInfoForClassObjectInstance<Object>(t);
}
public static void GetInfoForClassObjectInstance<T>(T t)
{
FieldInfo[] fields = t.GetType().GetFields
( BindingFlags.Public
| BindingFlags.NonPublic
| BindingFlags.Static
| BindingFlags.Instance
);
foreach (var field in fields)
{
string name = field.Name;
object temp = field.GetValue(t);
ToConsole(temp,field, name);
}
}
public static void GetInfoForClassType(Type T)
{
FieldInfo[] fields = T.GetFields
( BindingFlags.Public
| BindingFlags.NonPublic
| BindingFlags.Static
);
foreach (var field in fields)
{
string name = field.Name;
object temp;
if (field.IsStatic)
temp = field.GetValue(name); // Get value
else
temp = null; // we never really get here, just in case set to null
ToConsole(temp,field, name);
}
}
public static void ToConsole(object temp,FieldInfo field, string name)
{
// print out the data
if (field.IsPrivate)
Console.Write("private ");
if (field.IsPublic)
Console.Write("public ");
if (field.IsStatic)
Console.Write("static ");
if (temp != null)
{
// we cast according to temps type
if (temp is bool)
{
bool value = (bool)temp;
Console.WriteLine("bool " + name + " = " + value);
}
else if (temp is int)
{
int value = (int)temp;
Console.WriteLine("int " + name + " = " + value);
}
else if (temp is float)
{
float value = (float)temp;
Console.WriteLine("float " + name + " = " + value);
}
else if (temp is string)
{
string value = temp as string;
Console.WriteLine("string " + name + " = " + value);
}
else
Console.WriteLine(name + " = unspecified type of value to cast");
}
else
{
Console.WriteLine(name + " Failed to get value , dont think its possible this way ");
}
//_______
}
}
}
}
Resulting console output from running the program
Code:
//_________________________________________________
//Get info of the class type
//_________________________________________________
//public static (string) Name = bob
//public static (int) Height = 100
//private static (int) Width = 50
//_________________________________________________
//press a key to continue
//_________________________________________________
//Get info of a instatiatable class itself via passing a constructable type
//_________________________________________________
//public (string) Description = Not much to describe maybe
//private (int) Weight = 300
//public static (string) Name = bob
//public static (int) Height = 100
//private static (int) Width = 50
//_________________________________________________
//press a key to continue
//_________________________________________________
//Get info of a object instantiation of the class
//_________________________________________________
//public (string) Description = Something to describe now
//private (int) Weight = 300
//public static (string) Name = Fred
//public static (int) Height = 100
//private static (int) Width = 50
//_________________________________________________
//press a key to exit
Amendment
you can also get a basic Type description like so
Code:
/// <summary>
/// Writes to console basic information about a Type
/// usage MemberInfoToConsole( typeof(class_name) )
/// class name refers to SomeClass not its string represention
/// </summary>
public static void MemberInfoToConsole(Type T)
{
// get more info
var members = T.GetMembers();
Console.WriteLine("_____class_members_____");
foreach (var member in members)
{
var name = member.Name;
var val = member.MemberType;
Console.WriteLine(" " + val.ToString() + " " + name);
}
Console.WriteLine("_______________________");
}
Last edited by willmotil; June 23rd, 2014 at 02:00 AM.
Reason: amendments
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|