how to extract object .net - c#

I have a function that returns this object:
var result = AjaxUserToPlacePaging();
//this is the object
object[] objcs = new object[1];
objcs[0] = new
{
countHereNow = countHereNow.ToString(),
countWillBeHere = countWillBeHere.ToString(),
countWasHere = countWasHere.ToString()
};
How can I extract the information from the object?
for example
int count = result[0].countHereNow;
the resun i nned the function to be object is because
i using jquery
and with the jquery it takes the object and converting it into json

You will need to use Reflection:
string countHereNow = string.Empty;
Type objType = result[0].GetType();
PropertyInfo prop = objType.GetPropertyInfo("countHereNow");
countHereNow = prop.GetValue(result[0], null) as string ?? string.Empty;
Typically I think returning an anonymous type is a bad idea, but maybe you have a reason for it. Otherwise, I would create a custom class and just return an array of that class type.

I recommend making a custom class, and storing that, instead of just storing an anonymous type into an "object" directly.
objcs[0] = new MyClass
{
countHereNow = countHereNow.ToString(),
countWillBeHere = countWillBeHere.ToString(),
countWasHere = countWasHere.ToString()
};
That will allow you to cast it back, when you retrieve:
MyClass myClass = result[0] as MyClass;
if (myClass != null)
{
int count = myClass.countHereNow;
}
There are tricks you can do to work with anonymous types, but in most cases, they just make the code less maintainable.

You need to use reflection (or something that uses reflection) to get the contents of your anonymous object outside the function where you create it. Maybe you should create a class and return instances of that instead?

You're currently declaring the array as an object array
object[] objcs = new object[1];
Declare it as a implicitly typed (var) array
var objcs = new [];
That way the objects you actually adding to the array won't be of SystemObject type but will be implicitly typed

Related

Pass an array or list of different objects to a function for modification

Currently I am receiving an array of objects from a database.
object [] sqlResultData = DatabaseCall.Result();
This array of objects needs to be matched to class variables like this
CClassOfVars classVar = new CClassOfVars();
classVar.myProperty = sqlResultData[0];
classVar.myProperty1 = sqlResultData[1];
What i wish to do is pass the list of propertys on the class in order to a function and have the mapping from the object array occur automatically based on the order.
For example:
Method defined like this
FillData(object [] databaseValues, IList<object>())
Called like this
CClassOfVars classVar = new CClassOfVars();
object [] sqlResultData = DatabaseCall.Result();
FillData(sqlResultData, new List<object>(){classVar.myProperty,classVar.myProperty1});
The FillData function would hopefully type cast and set the values of myProperty and myProperty1 to the values in array locations of 0,1 etc...
Something like this
FillData(object [] databaseValues, IList<object> mapMe)
{
for (int i = 0; i < mapMe.Count; i++)
{
mapMe[i] = CastToTheCorrectType(mapMe[i], databaseValues[i]);
}
}
Cast to the correct type could look like this?? I took from here: cast object with a Type variable
public T CastToTheCorrectType<T>(T hackToInferNeededType, object givenObject) where T : class
{
return givenObject as T;
}
How can i pass a list of different object types to all have there values modified and assigned within a different function?
The matter you asking about is dark and difficult to be implemented through just a function. There are frameworks out there dealing with object relational mapping. If it is an option, install and learn some OR/M. If not ... well, there might be some dirty way.
You can use the JSON.NET library to do the heavy lifting for you. It's super easy to use and install through Nuget. My point is as follows.
Construct an anonymous object. Use the property names of the original object.
Fill it with the data from the object array. Spin a loop over the object array...
Serialize the anonymous object.
Deserialize the JSON string into the target type.
At this point, JSON.NET will handle property mapping for you.
List item
E.g. if your target type is Person you might do this:
var x = new
{
FirstName = String.Empty,
LastName = String.Empty
};
var persons = new List<Person>(sqlResultData.Length);
foreach (var record in sqlResultData)
{
x.FirstName = record[0];
x.LastName = record[1];
var s = JsonConvert.SerializeObject(x)`
var personX = JsonConvert.Deserialize<Person>(s);
persons.Add(person);
}

Extract type of object to use as generic <T>

I have a class VResult<T> that can be instantiated with
bool value = true;
VResult<bool> vr = new VResult<bool>(value);
If I don't know the type of value, I'd like to do something like
VResult<typeof value> = new VResult<typeof value>(value);
Is that possible?
The ultimate goal is to serialize/deserialize VResult<T>:
string json = JsonConvert.SerializeObject(new VResult<bool>(true));
where could be an object or basic datatype such as int or bool.
I'm using a data transfer object that adds
ValueTypeName = Value.GetType().Name;
and
ValueTypeNamespace = Value.GetType().Namespace;
properties so that on the receiving side I can use
JObject obj = JObject.Parse(json);
string vt = (string)obj["ValueTypeName"];
string vtn = (string)obj["ValueTypeNamespace"];
Type type = Type.GetType($"{vtn}.{vt}");
var value = Activator.CreateInstance(type);
value = obj["Value"];
VResult<typeof value> vr = new VResult<typeof value> (value); //not correct
to get all the Type information about value, but I just don't figure out how to get the generic <T> from value to pass it in the VResult<T> constructor;
You can create the generic instance like this:
object value = 1; //I don't know the runtime type of this
var genericType = typeof(VResult<>).MakeGenericType(value.GetType());
var genericInstance = Activator.CreateInstance(genericType,
new object[] { value });
And now you have a instance of VResult<int> with a value of 1. Is this what you were looking for?

Error when dynamically creating a generic list through reflection

when i run the program an error message pops up, it says: Object reference not set to an instance of an object from the methodinfo.invoke(data, null). what i want is create a dynamic generic collection in run time, depends on the xml file, it can be list<classname>, dictionary<string, classname>, customgenericlist<T>, etc.
Below are the codes: using list as a test subject.
data = InstantiateGeneric("System.Collections.Generic.List`1", "System.String");
anobj = Type.GetType("System.Collections.Generic.List`1").MakeGenericType(Type.GetType("System.String"));
MethodInfo mymethodinfo = anobj.GetMethod("Count");
Console.WriteLine(mymethodinfo.Invoke(data, null));
this is the code to instantiate the said datatype:
public object InstantiateGeneric(string namespaceName_className, string generic_namespaceName_className)
{
Type genericType = Type.GetType(namespaceName_className);
Type[] typeArgs = {Type.GetType(generic_namespaceName_className)};
Type obj = genericType.MakeGenericType(typeArgs);
return Activator.CreateInstance(obj);
}
Count is a property, not a method:
var prop = anobj.GetProperty("Count");
Console.WriteLine(prop.GetValue(data, null));
However, it would be better to cast to the non-generic IList:
var data = (IList)InstantiateGeneric("System.Collections.Generic.List`1",
"System.String");
Console.WriteLine(data.Count);
I also suggest talking in terms of Type, not magic strings:
var itemType = typeof(string); // from somewhere, perhaps external
var listType = typeof(List<>).MakeGenericType(itemType);
var data = (IList)Activator.CreateInstance(listType);
Console.WriteLine(data.Count);

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

Setting array values without knowing its type

I have an array of MyType s in an object similar to the setup below, I want to be able to set the value of an index on myObject as it is, I cannot change the declaration of MyFunction.
/*...*/
MyType [] myTypedArray = new MyType[100];
MyFunction(myTypedArray);
/*...*/
function MyFunction(object myObject)
{
myObject[0] = new MyType();
}
This, obviously, doesn't work but I cannot figure out how to get it to work. I cannot cannot use templates/generics it has to be all using reflection.
Note that I have to have myObjectArray, initially, as an "object" as this is how I receive it. I cannot do the initial cast to a object[]. I have tried casting myObjectArray to object[] and it doesn't work.
I also do not have access to MyType as a static type, only runtime.
Any advice would be great, thanks.
Declare myObjectArray as an array of objects:
object[] myObjectArray = myTypedArray;
myObjectArray[0] = new MyType(); // okay to go!
Alternatively, if for some reason myObjectArray must be an object you can say:
((object[])myObjectArray)[0] = new MyType();
These are smelly though. And beware of array covariance in C#!
Here's an implementation along the lines that you seem to be looking for:
void AddToArray(object array, int index) {
if(array == null) {
throw new ArgumentNullException();
}
Type arrayType = array.GetType();
if(!arrayType.IsArray) {
throw new InvalidOperationException("array must be an Array");
}
(Array)asArray = (Array)array;
if (index < 0 || index >= asArray.Length) {
throw new ArgumentOutOfRangeException("index");
}
object newInstance = Activator.CreateInstance(elementType);
asArray.SetValue(newInstance, index);
}
Invoke as:
AddToArray(myObjectArray, 0);
This assumes that elementType has a parameterless constructor. There are ways around this, but this should be enough to get you started. This method also probably needs a better name.
Again, this code is very smelly. I would suggest taking a step back and seeing if you can solve your problem using strongly-typed code.
You can cast the object to an Array and call SetValue on it, for a completely non-object-specific way of doing it. This'll box & unbox value types as appropriate & do all the necessary array type checking.
Try something like:
DayOfWeek[] myTypedArray = Enum.GetValues(typeof(DayOfWeek))
.OfType<DayOfWeek>().ToArray();
object myObject = myTypedArray;
object[] myObjectArray = // from object to object[]
(myObject as Array ?? new object[0]).OfType<object>().ToArray();
Type type = myObjectArray[0].GetType(); // unbox type
myObjectArray[0] = Activator.CreateInstance(type); // create another instance
Note you'll lose type safety.
In this case, you need a cast to object[]:
((object[])myObjectArray)[0] = new MyType();
((MyType[])myObjectArray)[0] = new MyType(); perhaps?

Categories

Resources