ExpandoObject object and GetProperty() - c#

I'm trying to write a generic utility for use via COM from outside .NET (/skip long story). Anyway, I'm trying to add properties to an ExpandoObject and I need to get PropertyInfo structure back to pass to another routine.
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Reflection;
public class ExpandoTest
{
public string testThis(string cVariable)
{
string cOut = "";
ExpandoObject oRec = new ExpandoObject { };
IDictionary<string, object> oDict = (IDictionary<string, object>)oRec;
oDict.Add(cVariable, "Test");
Trace.WriteLine(cVariable);
Trace.WriteLine(oDict[cVariable]);
PropertyInfo thisProp = oRec.GetType().GetProperty(cVariable);
if (thisProp != null)
{
cOut= "Got a property :)";
}
return cOut;
}
}
Why do I always get a null in in thisProp? I clearly don't understand but I've been staring at it for a day and I'm not getting anywhere. All help/criticism thankfully accepted!

While using an ExpandoObject it might look like you can add properties at runtime, it won't actually do that at the CLR level. That's why using reflection to get the property you added at runtime won't work.
It helps to think of an ExpandoObject as a dictionary mapping strings to objects. When you treat an ExpandoObject as a dynamic variable any invocation of a property gets routed to that dictionary.
dynamic exp = new ExpandoObject();
exp.A = "123";
The actual invocation is quite complex and involves the DLR, but its effect is the same as writing
((IDictionary<string, object>)exp)["A"] = "123";
This also only works when using dynamic. A strongly typed version of the code above results in a compile-time error.
var exp = new ExpandoObject();
exp.A = "123"; // compile-time error
The actual implementation of ExpandoObject can be found here.

Related

Getting a PropertyInfo of a dynamic object

I have a library that is doing a bunch of reflection work relying on the PropertyInfo of the classes it receives (to get and set values).
Now I want to be able to work with dynamic objects, but I can't find how to get the PropertyInfo of a dynamic's properties. I've checked the alternatives, but for those I'd need to change everywhere I use PropertyInfo to get/set values.
dynamic entity = new ExpandoObject();
entity.MyID = 1;
// - Always null
PropertyInfo p = entity.GetType().GetProperty("MyID");
// - Always null
PropertyInfo[] ps = entity.GetType().GetProperties();
// - These are called everywhere in the code
object value = p.GetValue(entity);
p.SetValue(entity, value);
Is it possible to get or create a PropertyInfo somehow just to be able to use it's GetValue() and SetValue() on a dynamic object?
Under the covers an ExpandoObject is really just a dictionary. You can get at the dictionary just by casting it.
dynamic entity = new ExpandoObject();
entity.MyID = 1;
if(entity.GetType() == typeof(ExpandoObject))
{
Console.WriteLine("I'm dynamic, use the dictionary");
var dictionary = (IDictionary<string, object>)entity;
}
else
{
Console.WriteLine("Not dynamic, use reflection");
}
You could modify your Mapping method to check if the object being passed in is dynamic and route through a different path that just iterates over the keys of the dictionary.
https://dotnetfiddle.net/VQQZdy

Convert from JSON object to expando object in c#

I have a JSON object, something like:
var jsonObject = {"att1" : "val1","att2" : "val2","att3" : "val3","att4" : "val4"}
I need to convert the same into an ExpandoObject.
I tried something like:
var expConverter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject<List<ExpandoObject>>(jsonObject,
expConverter);
But it is not giving me the desired result.
Can someone help me get the result?
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(jsonObject, expConverter);
Your JSON object isn't an array, so you can't serialize to a List<>. Just serialize to the object directly instead.
I see two issues, preventing the code to work:
The syntax of your jsonObject declaration is not correct. Declare it as a string and inside the string use the escape "backslash + double quote" for every double quote:var jsonObject = "{\"att1\": \"val1\",\"att2\": \"val2\",\"att3\": \"val3\",\"att4\": \"val4\"}";
Then, when using Newtonsoft.Json.Converters, instead of List<ExpandoObject> it needs to be ExpandoObject in the type argument, because an ExpandoObject is inside already a dictionary (not a list):dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(jsonObject, expConverter);
The following code fixes the two issues mentioned above and displays the key/value pairs on screen:
using System.Dynamic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
void Main()
{
// 1. this needs to be a string
var jsonObject =
"{\"att1\": \"val1\",\"att2\": \"val2\",\"att3\": \"val3\",\"att4\": \"val4\"}";
var expConverter = new ExpandoObjectConverter();
// 2. the correct type argument is ExpandoObject
dynamic obj =
JsonConvert.DeserializeObject<ExpandoObject>(jsonObject, expConverter);
// display the keys and values on screen
foreach (var o in obj)
{
Console.WriteLine($"{o.Key}, {o.Value}");
}
}
The output is:
att1, val1
att2, val2
att3, val3
att4, val4
Note: You can easily convert the expando to a dictionary (internally it already is an IDictionary) like:
var dict = new Dictionary<string, object>(obj);
That allows to do a dict.Dump() in LinqPad (not supported with ExpandoObject)
If you're trying it out in LinqPad press F4 and add the NUGET package Json.NET (if you don't have the paid version of LinqPad, NUGET function is limited there - try Rock.Core.Newtonsoft in this case).

Not able to get dynamic properties with reflection

A dynamic object is generated using a Json deserializing component (Jil) I am using, and I am able to access the properties directly. But I don't know their names in advance, so I am trying to get the names with reflection. I tried doing this:
var props = myDynObj.GetType().GetProperties();
but the page times out. Doesn't give me anything in debugger, just sits there doing nothing, or something and not telling me.
This even happens when I even do this:
var t = myDynObj.GetType();
But when I do this, it works:
var val = myDynObj.MyStaticValue1
Just can't really do anything else with it. Anyonw know why, and how I can get this to work?
Please allow me to note:
Before I get started, if you don't know the members already when you're parsing JSON, you should not be parsing into a dynamic object. The built-in .Net JavaScriptConverter class can parse JSON into a IDictionary<string, object> which would be much better for you.
However, if you still want to use dynamic objects for some reason:
If you want to stick with your current library: I dont know how exactly that class is working, and I'm not saying this is the best solution, but by looking at the source it jumps out to me that you can grab a list of the ObjectMembers keys using reflection.
Type t = typeof(JsonObject)
var fi = t.GetField("ObjectMembers", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
IEnumerable<string> keys = ((Dictionary<string, JsonObject>)fi.GetValue(obj)).Keys;
Edit: Seeing that JsonObject implements IDynamicMetaObjectProvider, the following method mentioned in this question will also work on it:
public static IEnumerable<string> GetMemberNames(object target, bool dynamicOnly = false)
{
var tList = new List<string>();
if (!dynamicOnly)
{
tList.AddRange(target.GetType().GetProperties().Select(it => it.Name));
}
var tTarget = target as IDynamicMetaObjectProvider;
if (tTarget !=null)
{
tList.AddRange(tTarget.GetMetaObject(Expression.Constant(tTarget)).GetDynamicMemberNames());
}else
{
if (ComObjectType != null && ComObjectType.IsInstanceOfType(target) && ComBinder.IsAvailable)
{
tList.AddRange(ComBinder.GetDynamicDataMemberNames(target));
}
}
return tList;
}
If you are open to trying a different JSON converter: try this class here: http://pastie.org/private/vhwfvz0pg06zmjqirtlxa I'm not sure where I found it (I can't take credit) but here is an example of how to use it how you want:
// Create dynamic object from JSON string
dynamic obj = DynamicJsonConverter.CreateSerializer().Deserialize("JSON STRING", typeof(object));
// Get json value
string str = obj.someValue;
// Get list of members
IEnumerable<string> members = (IDictionary<string, object>)obj).Keys
Personally I like using the second one, it is simple and easy to use - and builds off of the built in .Net JSON parser.

Complex anonymous objects to dynamic public properties

How do I make the complex structure of an anonymous object public inside a dynamic object?
Anonymous objects are flagged as internal, so I'm looking for a creative way to work around this.
// This is the library I control
public void SendObject() {
var anonymous = new {
Text = "Test",
SubItem = new {
SubText = "Bla",
SubSub = new {
SubSubText = "Baha"
}
}
};
}
dynamic dyn = ExposeAnonymous(anonymous); // Perform voodoo
var result = ExternalLibrary.GetSpecialProperty(dyn);
// External library I don't control
public object GetSpecialProperty(dynamic dyn) {
return dyn.SubItem.SubSub.SubSubText;
}
The problem is when sending the dynamic to other external libraries, that I don't control, you get an error like:
'object' does not contain a definition for 'SubItem'.
The problem is when sending the dynamic to other libraries,
... And there's the rub. Anonymous types are declared as internal by the C# compiler, which means that other assemblies don't have access to them.
Either stop using anonymous types, or use [InternalsVisibleToAttribute] to make the type visible to other assemblies. Within the assembly containing the type which creates the instance of the anonymous type, use:
[InternalsVisibleTo("ExternalLibrary")]
(I'd actually expect the issue to be on SubItem rather than SubSub...)
Anonymous types are internal and the DLR does the same accessibility analysis at run-time that the compiler does at compile-time. So you cannot access the anonymous type's members from another assembly using dynamic.
One option might be to use ExpandoObject:
dynamic a = new ExpandoObject();
a.Text = "Test";
a.SubItem = new ExpandoObject();
a.SubItem.SubText = "Blah";
a.SubItem.SubSub = new ExpandoObject();
a.SubItem.SubSub.Text = "Baha";
This is kind of ugly so, you could keep the anonymous type and use a helper method to recursively convert to an ExpandoObject:
public static dynamic ConvertToExpando(object obj)
{
IDictionary<string, object> expando = new ExpandoObject();
foreach(var pi in obj.GetType().GetProperties())
{
// there doesn't seem to be a way to know if it is an anonymous type directly. So I use IsPublic here.
if (pi.PropertyType.IsPublic)
{
expando[pi.Name] = pi.GetValue(obj);
}
else
{
expando[pi.Name] = ConvertToExpando(pi.GetValue(obj));
}
}
return expando;
}
This does the "Perform voodoo" that you need.
With my opensource framework ImpromptuInterface (in nuget) you can create complex expando graphs with an inline syntax.
dynamic New = Builder.New<ExpandoObject>();
dynamic dyn = New.Obj(
Text: "Test",
SubItem: New.Obj(
SubText: "Bla",
SubSub: New.Obj(
SubSubText: "Baha"
)
)
);

Get value of c# dynamic property via string

I'd like to access the value of a dynamic c# property with a string:
dynamic d = new { value1 = "some", value2 = "random", value3 = "value" };
How can I get the value of d.value2 ("random") if I only have "value2" as a string? In javascript, I could do d["value2"] to access the value ("random"), but I'm not sure how to do this with c# and reflection. The closest I've come is this:
d.GetType().GetProperty("value2") ... but I don't know how to get the actual value from that.
As always, thanks for your help!
Once you have your PropertyInfo (from GetProperty), you need to call GetValue and pass in the instance that you want to get the value from. In your case:
d.GetType().GetProperty("value2").GetValue(d, null);
public static object GetProperty(object target, string name)
{
var site = System.Runtime.CompilerServices.CallSite<Func<System.Runtime.CompilerServices.CallSite, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.GetMember(0, name, target.GetType(), new[]{Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(0,null)}));
return site.Target(site, target);
}
Add reference to Microsoft.CSharp. Works also for dynamic types and private properties and fields.
Edit: While this approach works, there is almost 20× faster method from the Microsoft.VisualBasic.dll assembly:
public static object GetProperty(object target, string name)
{
return Microsoft.VisualBasic.CompilerServices.Versioned.CallByName(target, name, CallType.Get);
}
Dynamitey is an open source .net std library, that let's you call it like the dynamic keyword, but using the a string for the property name rather than the compiler doing it for you, and it ends up being equal to reflection speedwise (which is not nearly as fast as using the dynamic keyword, but this is due to the extra overhead of caching dynamically, where the compiler caches statically).
Dynamic.InvokeGet(d,"value2");
The easiest method for obtaining both a setter and a getter for a property which works for any type including dynamic and ExpandoObject is to use FastMember which also happens to be the fastest method around (it uses Emit).
You can either get a TypeAccessor based on a given type or an ObjectAccessor based of an instance of a given type.
Example:
var staticData = new Test { Id = 1, Name = "France" };
var objAccessor = ObjectAccessor.Create(staticData);
objAccessor["Id"].Should().Be(1);
objAccessor["Name"].Should().Be("France");
var anonymous = new { Id = 2, Name = "Hilton" };
objAccessor = ObjectAccessor.Create(anonymous);
objAccessor["Id"].Should().Be(2);
objAccessor["Name"].Should().Be("Hilton");
dynamic expando = new ExpandoObject();
expando.Id = 3;
expando.Name = "Monica";
objAccessor = ObjectAccessor.Create(expando);
objAccessor["Id"].Should().Be(3);
objAccessor["Name"].Should().Be("Monica");
var typeAccessor = TypeAccessor.Create(staticData.GetType());
typeAccessor[staticData, "Id"].Should().Be(1);
typeAccessor[staticData, "Name"].Should().Be("France");
typeAccessor = TypeAccessor.Create(anonymous.GetType());
typeAccessor[anonymous, "Id"].Should().Be(2);
typeAccessor[anonymous, "Name"].Should().Be("Hilton");
typeAccessor = TypeAccessor.Create(expando.GetType());
((int)typeAccessor[expando, "Id"]).Should().Be(3);
((string)typeAccessor[expando, "Name"]).Should().Be("Monica");
Much of the time when you ask for a dynamic object, you get an ExpandoObject (not in the question's anonymous-but-statically-typed example above, but you mention JavaScript and my chosen JSON parser JsonFx, for one, generates ExpandoObjects).
If your dynamic is in fact an ExpandoObject, you can avoid reflection by casting it to IDictionary, as described at http://msdn.microsoft.com/en-gb/library/system.dynamic.expandoobject.aspx.
Once you've cast to IDictionary, you have access to useful methods like .Item and .ContainsKey
The GetProperty/GetValue does not work for Json data, it always generate a null exception, however, you may try this approach:
Serialize your object using JsonConvert:
var z = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(request));
Then access it directly casting it back to string:
var pn = (string)z["DynamicFieldName"];
It may work straight applying the Convert.ToString(request)["DynamicFieldName"], however I haven't tested.
d.GetType().GetProperty("value2")
returns a PropertyInfo object.
So then do
propertyInfo.GetValue(d)
To get properties from dynamic doc
when .GetType() returns null, try this:
var keyValuePairs = ((System.Collections.Generic.IDictionary<string, object>)doc);
var val = keyValuePairs["propertyName"].ToObject<YourModel>;
This is the way i ve got the value of a property value of a dinamic:
public dynamic Post(dynamic value)
{
try
{
if (value != null)
{
var valorCampos = "";
foreach (Newtonsoft.Json.Linq.JProperty item in value)
{
if (item.Name == "valorCampo")//property name
valorCampos = item.Value.ToString();
}
}
}
catch (Exception ex)
{
}
}
Some of the solutions were not working with a valuekind object that I obtained from a json string, maybe because I did not have a concrete type in my code that was similar to the object that I would obtain from the json string, so how I went about it was
JsonElement myObject = System.Text.Json.JsonSerializer.Deserialize<JsonElement>(jsonStringRepresentationOfMyObject);
/*In this case I know that there is a property with
the name Code, otherwise use TryGetProperty. This will
still return a JsonElement*/
JsonElement propertyCode = myObject.GetProperty("Code");
/*Now with the JsonElement that represents the property,
you can use several methods to retrieve the actual value,
in this case I know that the value in the property is a string,
so I use the GetString method on the object. If I knew the value
was a double, then I would use the GetDouble() method on the object*/
string code = propertyCode.GetString();
That worked for me
In .Net core 3.1 you can try like this
d?.value2 , d?.value3
Similar to the accepted answer, you can also try GetField instead of GetProperty.
d.GetType().GetField("value2").GetValue(d);
Depending on how the actual Type was implemented, this may work when GetProperty() doesn't and can even be faster.
In case you have a dynamic variable such as a DapperRow for example you can first build up an ExpandoObject, then cast the Expando into an IDictionary<string, object>. From then on, getting a value via the name of a property is possible.
Helper method ToExpandoObject:
public static ExpandoObject ToExpandoObject(object value)
{
IDictionary<string, object> dapperRowProperties = value as IDictionary<string, object>;
IDictionary<string, object> expando = new ExpandoObject();
if (dapperRowProperties == null)
{
return expando as ExpandoObject;
}
foreach (KeyValuePair<string, object> property in dapperRowProperties)
{
if (!expando.ContainsKey(property.Key))
{
expando.Add(property.Key, property.Value);
}
else
{
//prefix the colliding key with a random guid suffixed
expando.Add(property.Key + Guid.NewGuid().ToString("N"), property.Value);
}
}
return expando as ExpandoObject;
}
Sample usage, I have marked in bold the casting which gives us access in the example, I have marked the important bits with the ** letters:
using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
foreach (var dynamicParametersForItem in dynamicParametersForItems)
{
var idsAfterInsertion = (await connection.QueryAsync<object>(sql, dynamicParametersForItem)).ToList();
if (idsAfterInsertion != null && idsAfterInsertion.Any())
{
**var idAfterInsertionDict = (IDictionary<string, object>) ToExpandoObject(idsAfterInsertion.First());**
string firstColumnKey = columnKeys.Select(c => c.Key).First();
**object idAfterInsertionValue = idAfterInsertionDict[firstColumnKey];**
addedIds.Add(idAfterInsertionValue); //we do not support compound keys, only items with one key column. Perhaps later versions will return multiple ids per inserted row for compound keys, this must be tested.
}
}
}
In my example, I look up a property value inside a dynamic object DapperRow and first convert the Dapper row into an ExpandoObject and cast it into a dictionary property bag as shown and mentioned in other answers here.
My sample code is the InsertMany method for Dapper extension I am working on, I wanted to grab hold of the multiple ids here after the batch insert.
Use dynamic with Newtonsoft.Json.JsonConvert.DeserializeObject:
// Get JSON string of object
var obj = new { value1 = "some", value2 = "random", value3 = "value" };
var jsonString = JsonConvert.SerializeObject(obj);
// Use dynamic with JsonConvert.DeserializeObject
dynamic d = JsonConvert.DeserializeObject(jsonString);
// output = "some"
Console.WriteLine(d["value1"]);
Sample:
https://dotnetfiddle.net/XGBLU1

Categories

Resources