How would I get the namespace, class, method and name of a parameter or variable. For example:
namespace TheNamespace
{
class Theclass
{
void Thing()
{
string thevariable = "";
string b = thevariable.GetFullName();
// b would be equal to TheNamespace.Theclass.Thing.thevariable
}
}
}
How would I do this
Parameters and variables don't have namespace/class name so to get string you are looking for you simply combine Can you use reflection to find the name of the currently executing method? and get name of a variable or parameter:
string b =
System.Reflection.MethodBase.GetCurrentMethod().Name + "." +
nameof(thevariable);
Related
In my source code for a C# class, I want to use the values of parameters defined within some other class. I want to do it without "hardwiring" the other class name into the source code (e.g., without writing something like "class2.variable").
Rather, I want to pass the name of that other class as a character string at runtime.
I am using C# within Unity. So I want to set the name of the other class as a public string within the Inspector of Unity.
For example, consider these two separate scripts:
using UnityEngine ;
public class ScriptA : ScriptableObject {public static int fred = 5 ; }
and
using System;
using System.Reflection;
using UnityEngine;
public class ScriptB : MonoBehaviour {
object item;
private static string instance;
void Start() {
instance = "ScriptA";
item = ScriptableObject.CreateInstance(Type.GetType(instance));
Type myType = item.GetType();
foreach (FieldInfo info in myType.GetFields())
{
string infoName = info.Name; //gets the name of the field
Debug.Log (" info = " + infoName);
}
}
}
ScriptB works OK ; it accesses ScriptA just from the string "instance", as evidenced by the fact that
the name "fred" to appears in the console.
But how do I access the value of "fred" ; how do I make the number "5" appear on the console?
I have been trying for two days. I have searched extensively for an answer.
Can somebody please help?
FieldInfo has a GetValue method :
public abstract object GetValue(
object obj
)
Try:
Type myType = item.GetType();
foreach (FieldInfo info in myType.GetFields())
{
string infoName = info.Name; //gets the name of the property
Console.WriteLine(" Field Name = " + infoName +"and value = "+ info.GetValue(null));
}
How can I get the name of the original property name which is passed as a parameter to a method?
class TestA
{
public string Foo { get; set; }
public TestA()
{
Foo = "Bar";
TestB.RegisterString(Foo);
}
}
class TestB
{
public static void RegisterString(string inputString)
{
// Here I want to receive the property name that was used
// to assign the parameter input string
// I want to get the property name "Foo"
}
}
You can add an argument with the nameof keyword. Not sure why you would want that anyway:
TestB.RegisterString(Foo, nameof(Foo));
This will pass in "Foo" as the second argument. There is no way to automate this, so you don't need to call nameof yourself, which makes doing this quite useless.
If you would call this from the Foo property, you could use the CallerMemberNameAttribute, which will put in the caller's name. The compiler will set the correct value, so you don't have to supply this yourself in the calling method.
public static void RegisterString( string inputString
, [CallerMemberName] string caller = null
)
{
// use caller here
}
That makes more sense to me.
Background: a class constructor can have parameters whose names are the same as its properties, and resolve the assignment using the this keyword. Simplified example:
public MyClass(string Thing1, string Thing2)
{
this.Thing1 = Thing1;
this.Thing2 = Thing2;
}
What about object initializers? I want to instantiate an object, setting properties Amount and Percentage from parameters of the same name passed to the current method.
How can I qualify Amount and Percentage to distinguish the object properties from the method parameters?
I guess you're not using object initializer, instead you just have the braces with parameter assigned to itself.. With object initializers, it just works.
Here is the minimal repro:
private static void Dosomething(string Name)
{
{ Name = Name };//Assignment made to same variable error
var test = new TestClass{ Name = Name };//Works fine
}
class TestClass
{
public string Name { get; set; }
}
I need to get the property name of a static property dynamically called as a parameter.
Here is my Portable Class Library code:
public partial class Test
{
public Test()
{
string staticPropName = Test.GetPropName(Test.Row); // result must be "Row" without additional string
System.Diagnostics.Debug.WriteLine("propName=" + staticPropName);
}
public static int Row { get; set; }
public static string GetPropName(object Property)
{
return "Row"; // using reflection
}
}
I don't know the name of the property and I don't want to define it with an additional string.
You can't do that - when function is called it gets value of the property and have no idea where this value come from. Your sample is equivalent of
string staticPropName = Test.GetPropName(42);
which nobody would expect to return name.
You can try to require Expression as argument so you can actually inspect what method get called with like following staring point (https://stackoverflow.com/questions/1011109/how-do-you-get-the-name-of-the-property):
public static string GetPropName<TResult>(Expression<Func<TResult>> expression)
{
MemberExpression body = (MemberExpression)expression.Body;
return body.Member.Name;
}
string staticPropName = Test.GetPropName(()=> Test.Prop);
Note that you need checks to make sure expression is just one you expect and not something like () => Test + 42 or more complex one and report nice error.
I have an assembly asdf.dll and it has a class 'Class1'.
How can I get the type of Class1?
string a = "Class1"; //Class1 is the name of class in asdf.dll
string typeString = typeof(Class1).FullName; // here I only have the string Class1 and not Class Class1
AssemblyName assemblyName = AssemblyName.GetAssemblyName("asdf.dll");
Type type = Type.GetType(typeString + ", " + assemblyName);
How can I get the type of a class from a string holding the class name?
Type t = Type.GetType("MyDll.MyClass,Mydll")
where MyDll.MyClass is the class Location of your desire class/Form. Mydll is the your dll name. which u want to Call.
typeof(Class1).FullName is already the fully qualified name.
Try just passing that, or using the Type.Name property instead.