How to get variable name of object inside class? - c#

I have class A which stores ref to object B in BObject variable.
public class A
{
public B BObject;
}
I want to get BObject ( name of variable ) in B class constructor.
Is there any way to do this ?
Purpose of doing it: I want to create ODBCFramework and I want to get Table Name based on Variable Name. ( Like in EntityFramework Context )
Update: I want to handle it in C#5.

You can use C#-6 nameof operator:
var a = new A();
string bName = nameof(a.B);
Note that generally attempting to relay on a run-time name of a property/field for table lookup seems like a bad idea.

There is no way to do what you want.
You cannot find the name of whatever it is that is storing the reference to your object, that information is simply not available.
Basically, this:
var x = new BObject();
// from inside BObject, get the name "x"
is not possible. The fact that you have stored it in a field of another object changes nothing, it simply cannot be done.
You need to have a way to explicitly tell that object which table name it should use.

Can you use the PropertyInfo class?
var a = B.GetInfo().GetProperties();
foreach(PropertyInfo propertyInfo in a)
string name = propertyInfo.Name

#Damien_The_Unbeliever give me some points to solve my problem. And I tried this, and it works.
public class A
{
public B BObject { get; set; }
public A()
{
var BTypeProperties = this.GetType().GetProperties().Where(x => x.PropertyType == typeof(B));
foreach (var prop in BTypeProperties)
{
prop.SetValue(this, new B(prop.Name));
}
}
}
public class B
{
string _propName;
public B(string propertyName)
{
_propName = propertyName;
}
}
Also, to be clear in answer:
#Yuval Itzchakov suggested that in C#6 solution is:
var a = new A();
string bName = nameof(a.B);

Related

How to cast PropertyInfo to its own type using reflection

I am trying to use reflection for getting the property name declared and its value, I am able to get the declared property name using property info the main concern I am having is I want to get the value for the property and I don't know the object type so I cant cast directly.
I know we need to use item.GetValue(object) but here the object, I need to pass using reflection.
For example, if you see the below code
Class structure
public abstract class ObjectInputs{}
public class ValveInputs : ObjectInputs
{
public Conditions Conditions { get; set; } = new Conditions();
}
public class Conditions :IExportable
{
[CanExportAttribute]
public string north {get;set;}
}
Method
public void Append(Scenario scenario)
{
var scenarioInputs = (commonDomain.ObjectInputs)scenario.Inputs; // ObjectInputs is an abstract class
var exportableInputs = scenarioInputs.GetType().GetProperties().Where(x =\> typeof(IExportable).IsAssignableFrom(x.PropertyType)); // I extraced property having interface IExportable
var listOfExportableProperties = new ScenarioExtract();
foreach (var exportableInput in exportableInputs)
{
var allProperties = ((System.Reflection.TypeInfo)exportableInput.PropertyType).DeclaredProperties; // Got all the property details
var propertyHavingAttribute = allProperties.Where(x =\> x.CustomAttributes.Where(z =\> z.AttributeType == typeof(CanExportAttribute)).Any()).ToArray(); // Got the properties which i need to extract.
The issue is here, if i do this then its creating a new instance and the values of each properties are set to default. I want to cast the exportableInput to its type (I cant hard code the type casting) so that i can use the value below.
object destination = Activator.CreateInstance(scenarioInputs.GetType());
foreach (var item in propertyHavingAttribute)
{
var detail = new InputPropertyDetail { InputName = item.Name, InputValue = \*\*item.GetValue(destination).ToString() \*\*}; \*\*want to use value here\*\*
listOfExportableProperties.PropertyDetails.Add(detail);
}
}
spreadsheetBuilder.AppendComponenet(listOfExportableProperties);
}
If you're using Activator.CreateInstance, it will always create a new instance (as the name inplies) with default values. Instead you must use the value of the exportableInput property.
object destination = exportableInput.GetValue(scenarioInputs);
Then you can get the actual value of the exportable property of the instance with InputValue = item.GetValue(destination).ToString().

Reflection - SetValue from deep context

I am facing an issue, surely due to my lack of knowledge in the reflection process, while trying to set a "complex" class hierarchy based on Json files.
Here are my main model :
public class Names
{
public Weapons Weapons { get; set; }
public Armors Armors { get; set; }
public Utilities Utilities { get; set; }
public Names()
{
Weapons = new Weapons();
Armors = new Armors();
Utilities = new Utilities();
}
}
Each of them having a list of sub-model like this:
public class Weapons
{
public BattleAxe BattleAxe { get; set; } = new BattleAxe();
public Bomb_Missile Bomb_Missile { get; set; } = new Bomb_Missile();
// etc... Around 20 to 25
}
And finally the ended model which is the exact equivalent of each json files but may have very different properties :
public class BattleAxe
{
public string[] Normal { get; set; } = new string[0];
public string[] DescriptiveAdjective { get; set; } = new string[0];
public string[] Material { get; set; } = new string[0];
public string[] Type { get; set; } = new string[0];
public string[] Title { get; set; } = new string[0];
public string[] Of { get; set; } = new string[0];
public string[] NormalForTitle { get; set; } = new string[0];
}
Since the MS Json deserializer does not support the conversion to a $type as Newtonsoft before, I tried to populate the values using reflection too like this (I've removed all the null-check for code readability) :
public static void Load()
{
Names = new Names();
foreach (var category in Names.GetType().GetProperties())
{
if (category is not null && !(category.GetGetMethod()?.IsStatic ?? false))
{
var categoryType = category.PropertyType;
foreach (var item in category.PropertyType.GetProperties())
{
var itemType = item.PropertyType;
var subTypeData = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(File.ReadAllText($"./Assets/Names/{categoryType.Name}/{itemType.Name}.json"));
var concreteObj = Activator.CreateInstance(itemType);
foreach (var key in subTypeData.Keys)
{
if (itemType.GetProperty(key) is not null && concreteObj is not null)
{
var prop = concreteObj.GetType().GetProperty(key);
var convertedValue = ConvertJsonType(subTypeData[key], subTypeData[key].ValueKind, out var isReferenceType);
// It fails here
prop.SetValue(
isReferenceType ? convertedValue : null,
!isReferenceType ? convertedValue : null
);
}
}
item.SetValue(concreteObj, null);
}
}
}
}
So it fails at the prop.SetValue(...) of the deepest object in the hierarchy with a different error depending on the type of value to set.
If it is a reference, it throws a System.Reflection.TargetException : 'Object does not match target type' Exception
And if it is value, it throw a System.Reflection.TargetException : 'Non-static method requires a target.'
Knowing that I do not have problems around the deserialization as shown here, only the fact that I use a dynamic type (and my instinct tells me it is actually the problem...)
I do not add the ConvertJsonType(...) body as it is functional and really simple
I am more interested in the 'why' than the 'how' so if you can explain me the 'theory' behind the problem, that would help quite a lot :)
Thank you!
PS: I know I can simplify the things in a more readable/performant way but I must achieve it with reflection for personal learning :)
Same for the System.Text.Json namespace, I do not intend to switch back to Newtonsoft for that
When calling SetValue(instance, value) you should pass the object which property should be set.
It's a wild guess, but you could try this:
prop.SetValue(concreteObj,
!isReferenceType ? convertedValue : null);
Because you want to fill the properties of concreteObj, not the value it self.
If you look at the object prop it was a return value of concreteObj.GetType().GetProperty(key);. If you look at it close, The GetProperty is a method from Type which isn't bound to any instance. So that's why you need to pass the instance of the object as the first parameter.
I mean this in a positive way: The itemType.GetProperty(key) is called every iteration, it will be the same value each iteration, you could bring it before the loop.
As docs state TargetException is thrown when:
The type of obj does not match the target type, or a property is an instance property but obj is null.
Passing null for obj in SetValue is valid when you are trying to set value for static property, not an instance one. Property type being a reference one has nothing to do with property being instance or static one so your call should look something like:
prop.SetValue(concreteObj, convertedValue);
Also your item.SetValue(concreteObj, null); does not look right cause concreteObj should be second argument in this call. Something like this:
item.SetValue(Names, concreteObj);
Also if you want only instance properties you can provide BindingFlags to get only instance properties:
foreach (var category in Names.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
Also I would say that category is not null check is redundant so in pair with providing BindingFlags you should remove the if completely.

How can I search for a specific struct value? Maybe a better approach?

I'm trying to find a struct I created earlier that has a specific value. Once I found it, I want to set variables on that struct. I don't know how to do this. Is there a better way of doing this? Maybe classes? Or should structs work?
For example, my struct:
public struct MyTest
{
public string device;
public string status;
public string revision;
public string number;
public string ledmo;
}
My Test Code:
MyTest thisTest=new MyTest();
thisTest.device=blah;
thisTest.number=blah2;
MyTest thisTest2=new MyTest();
thisTest2.device=blah5;
thisTest2.number=blah6;
//Another Part in my code.
//Need to find the MyTest Structure that 'device' variable = the string 'blah'
var Foundit=MyTest.find(device==blah);
Foundit.revision=blah9999;
I'd use a class, because Mutable structs are evil
Basically, because every struct is copied, even if you do find the right struct, you'll only ever change one copy. Lets say MyTest.find finds thisTest2 what happens is this
var Foundit = MyTest.Find(device==blah);
// The line above has made a copy of thisTest2, that copy is in FoundIt
Foundit.revision = "blah9999";
// You've changed revision in the copy of thisTest2,
// therefore the contents of thisTest2 remain unchanged
To do this with a class you'll need to keep every instance of the class you create in a list or other data structure, so you know you can look it up.
If you do this you also need to tell the list when you're finished with each object, otherwise they'll hang around forever and never get garbage collected.
Before I go any further, are you sure this is the best way to solve this problem?
Anyway, say your class is MyData, you can put a static factory method on this called Create, which will put each new MyData object into a list.
public class MyData
{
private static List<MyData> allMyDatas = new List<MyData>();
public static IEnumerable<MyData> AllInstances
{
get {return allMyDatas;}
}
public string Device {get; set;}
public string Status {get; set;}
public string Revision {get; set;}
public string Number {get; set;}
public string Ledmo {get; set;}
private MyData() // Private ctor ensures only a member
{ // function can create a new MyData
}
public static MyData Create()
{
var newData = new MyData();
allMyDatas.Add(newData);
return newData;
}
public static void Delete(MyData itemToRemove)
{
allMyDatas.Remove(itemToRemove);
}
}
Everywhere you use a MyData you'll need to Delete it when you're finished with it.
Your code becomes
var thisTest = MyData.Create();
thisTest.Device = "blah";
thisTest.Number = "blah2";
var thisTest2 = MyData.Create();
thisTest2.Device = "blah5";
thisTest2.Number = "blah6";
//Another Part in my code.
//Need to find the MyData Structure that 'device' variable = the string 'blah'
var Foundit = MyData.AllInstances.FirstOrDefault(md => md.Device == "blah");
if(Foundit != null)
Foundit.Revision = "blah9999";
Changing FoundIt now also changes thisTest
P.S.: It's important that nothing outside MyData can new an instance of MyData. If it could, then there would be an instance of MyData that you couldn't find in AllInstances. Declaring the constructor private means a compiler error will be generated if code outside MyData tries something like var someData = new MyData
To be able to find instances of an object created earlier, these instances need to be saved somewhere.
One solution would be to put them into a list and later search that list:
var list = new List<MyTest>();
MyTest thisTest=new MyTest();
thisTest.device=blah;
thisTest.number=blah2;
list.Add(thisTest);
MyTest thisTest2=new MyTest();
thisTest2.device=blah5;
thisTest2.number=blah6;
list.Add(thisTest2);
Now you can search using LINQ:
var foundItems = list.Where(x => x.device == "blah");
foreach(var foundItem in foundItems)
{
foundItem.revision = "blah9999";
}
Please note:
This only works when you use classes instead of structs as Binary Worrier points out in his comment.
In this case a class would work better because of the dynamic string size and the fact that there are so many strings.
With your test code, you should be storing a List<MyTest> somewhere in that class and adding thisTest and thisTest2 to the list. You can later retrieve specific values (or all the values of a certain device) with the FindAll or similar methods.
List<MyTest> list = new List<MyTest>();
//add MyTests here...
var foundIt = list.FindAll(x => x.device == "blah");
You can use lists and Linq for that.
var test = new List<MyTest>();
//Add some items
var foundIt = test.SingleOrDefault(test => test.device == "abc");//Maximum one
if(foundIt != null)//Use a class for MyTest.
foundIt.device = "123"

.NET/C#: Is it possible to replace one object in memory with another?

Take this code for example:
class Jooky
{
static long Last;
public Jooky() { Id += Last++; }
public long Id { get; set; }
public string Name { get; set; }
}
class Flooky
{
public Flooky() { Jooky1 = new Jooky(); Jooky2 = new Jooky(); }
public Jooky Jooky1 { get; set; }
public Jooky Jooky2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Flooky> Flookies = new List<Flooky>();
//I build a collection of flookies to emulate the service call of
//FlookyProxy.GetAllFlookies().
for (int i = 0; i < 5; i++) Flookies.Add(new Flooky());
//This makes a collection of all the jookies in all the flookies.
var Jookies = Flookies.Select(f => f.Jooky1).Union(Flookies.Select(f => f.Jooky2));
//I get the jooky.
Jooky Jooky = Jookies.Single(j => j.Id == 2);
//Fig 1: I just got a jooky out of the collection. One of the flookies
//has a reference to this jooky. I want to set the jooky to a new
//reference, but still want the same flooky to reference it.
Jooky = new Jooky { Name = "Bob" };
//I get the jooky again
Jooky = Jookies.Single(j => j.Id == 2);
//However, this writes an empty string because only the Jooky variable
//I previously declared was affected.
Console.WriteLine(Jookies.Single(j => j.Id == 2).Name);
//Basically, I want the code in Fig 1 above to be the same as:
//Flooy.Jooky = new Jooky { Name = "Bob" };
Console.Read();
}
}
Basically, variable A is referencing Aa in memory and variable B is referencing object Bb in memory. I want to make A reference the same object in memory as B without going like A = B;. Instead, I want to replace the physical object in memory with another, ultimately going like Aa = Bb;.
Is this at all possible?
Update: Primary rule: I cannot reference the flooky directly, so I can't be all like Flooky.Jooky1 = new Jooky() or Flookies[3].Jooky1 = new Jooky().
Maybe this is possible with unsafe code as suggested by havardhu, but it's definitely not possible with safe code. It's important to understand why doing what you're trying to do is unsafe. Not only does it break encapsulation, it breaks type safety. Consider this example.
class Me : IHaveCar
{
BuickCentury myCentury = new BuickCentury(2004);
public Car Car { get { return myCentury; } }
public void Drive()
{
myCentury.CruiseWithAuthority();
}
}
class EvilOilChangeService
{
public void ChangeOil(IHaveCar customer)
{
Car car = customer.Car;
// here's the fictional "replace object in memory" operator
car <<== new VolkswagenBeetle(2003);
}
}
The EvilOilChangeService can create a situation where myCentury is referencing a VolkswagenBeetle! I'm going to be in trouble when I try to go for a Drive because a VolkswagenBeetle just can't CruiseWithAuthority like a BuickCentury can (especially when the driver is 6'2")
Even in C/C++ which allows willy-nilly memory access, I would still be quite surprised by code that does what you want to do. This is why most of the other answers are suggesting a different approach or design.
Change:
//Jooky = new Jooky { Name = "Bob" };
Jooky.Name = "Bob" ;
The resullt of the .Single() is a reference to an instance (object). You were just overwriting the reference with one to a new object. The old object was not changed or overwritten.
To understand what's going on, and to adjust what you are aiming for, look up "Value Type and Reference Type". Lots of reading to do.
After reading the comment:
If your Details (Jookies) are going to change independently of their Owners (the Flookies) then you just need another layer of indirection.
A simple suggestion:
do not store references to the details (since they will change)
store a DetailId instead (JookyId1, JookyId2)
keep the Details in a Dictionary (Dictionary<int,Jooky>)
create a (readonly) property in Owner to get Detail1 by looking it up in the dictionary.
You can write unsafe code in C# which enables you to operate on direct memory.
Have a look here for details:
Pointers and arrays in C#
You'll notice that you can use the familiar pointers (*) and addresses (&) from C and C++.
Here's an example of an unsafe swap, which I think is what you're after:
Unsafe swap in C#
Jooky = new Jooky { Name = "Bob" };
Flookies[0].Jooky1=Jooky;
If you want to replace and object with another without just assigning references you just to copy all the data fields to the other object. Not sure if i have understood your question correctly.
When you're working with references, every assignment to a reference changes the object that reference points to.
So, when you say:
Jooky Jooky = Jookies.Single(j => j.Id == 2);
you're creating a reference to the Jookie with Id == 2. And then, when you say Jooky = new Jooky { Name = "Bob" };, you're telling that reference you created to point to the Jooky you have just created instead.
So, if you want to set a new value to the Jookie1 property (wich is a placeholder for a reference to a Jookie object) of the Flookies[0] object, you got to say:
Flookies[0].Jooky1 = new Jooky { Name = "Bob" }; (as stated by #Ashley John's answer).
That way, you're telling the Flookies[0].Jooky1 reference to point to the new Jooky { Name = "Bob" }; object.
For further explanation, see http://msdn.microsoft.com/en-us/library/ms173104.aspx .
If you have access to the Jookie class, you could add a property that holds the parent Flookie of the Jookie:
class Jooky
{
static long Last;
public Jooky(Flooky parent)
{
Id += Last++;
Parent = parent;
}
public long Id { get; set; }
public string Name { get; set; }
public Flooky Parent { get; private set; }
}
and then access the parent Flookie and change it's Jookie1 property:
Flookie flookie = Jookies.Single(j => j.Id == 2).Parent;
flookie.Jookie1 = new Jookie { Name = "Bob" }

Accessing C# property name or attributes

I would like to automatically generate SQL statements from a class instance. The method should look like Update(object[] Properties, object PrimaryKeyProperty). The method is part of an instance (class, base method - generic for any child). Array of properties is an array of class properties, that will be used in update statement. Property names are equal to table field names.
The problem is that I can't get property names.
Is there any option to get a property name inside class instance?
sample:
public class MyClass {
public int iMyProperty { get; set; }
public string cMyProperty2 { get; set; }
{
main() {
MyClass _main = new MyClass();
_main.iMyProperty.*PropertyName* // should return string "iMyProperty"
{
I am aware of PropertyInfo, but I don't know hot to get the ID of a property from GetProperties() array.
Any suggestion?
Just wrote an implementation of this for a presentation on lambdas for our usergroup last Tuesday.
You can do
MembersOf<Animal>.GetName(x => x.Status)
Or
var a = new Animal()
a.MemberName(x => x.Status)
the code:
public static class MembersOf<T> {
public static string GetName<R>(Expression<Func<T,R>> expr) {
var node = expr.Body as MemberExpression;
if (object.ReferenceEquals(null, node))
throw new InvalidOperationException("Expression must be of member access");
return node.Member.Name;
}
}
Link to the presentation and code samples.
Also in SVN (more likely to be updated): http://gim-projects.googlecode.com/svn/presentations/CantDanceTheLambda
I found a perfect solution in This Post
public static string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
{
return (propertyExpression.Body as MemberExpression).Member.Name;
}
And then for the usage :
var propertyName = GetPropertyName(
() => myObject.AProperty); // returns "AProperty"
Works like a charm
You can do something like this:
Type t = someInstance.getType();
foreach (MemberInfo mi in t.GetMembers())
{
if (mi.MemberType == MemberTypes.Property)
{
Console.WriteLine(mi.Name);
}
}
to get all the property names for instance's type.
You can get the name (I assume that's what you meant by ID) of a property using PropertyInfo.Name. Just loop through the PropertyInfo[] returned from typeof(className).GetProperties()
foreach (PropertyInfo info in typeof(MyClass).GetProperties())
{
string name = info.Name;
// use name here
}
Since you already have an explicit handle to the specific property you want, you know the name - can you just type it?
Not 100% sure if this will get you what you're looking for, this will fetch all properties with [Column] attribute inside your class:
In the datacontext I have:
public ReadOnlyCollection<MetaDataMember> ColumnNames<TEntity>( )
{
return this.Mapping.MappingSource.GetModel(typeof(DataContext)).GetMetaType(typeof(TEntity)).DataMembers;
}
Fetching the table column-names that are properties inside the class:
MyDataContext db = GetDataContext();
var allColumnPropertyNames = db.ColumnNames<Animal>().Where(n => n.Member.GetCustomAttributes(typeof(System.Data.Linq.Mapping.ColumnAttribute), false).FirstOrDefault() != null).Select(n => n.Name);
Let's say (from the first sample, method update of a class MyClass):
public class MyClass {
public int iMyStatusProperty { get; set; }
public int iMyKey { get; set; }
public int UpdateStatusProperty(int iValue){
this.iMyStatusProperty = iValue;
return _Update( new[iMyStatusProperty ], iMyKey); // this should generate SQL: "UPDATE MyClass set iMyStatusProperty = {iMyStatusProperty} where iMyKey = {iMyKey}"
}
{iMyStatusProperty} and {iMyKey} are property values of a class instance.
So, the problem is how to get property name (reflection) from a property without using names of properties as strings (to avoid field name typos).

Categories