I have a dynamic type object and I want to get all the values of every property from the object.
dynamic row = ....
I am using property.GetValue(row, null) throws a RuntimeBinderException.
How can I retrieve this value?
This will iterate thru all public properties:
dynamic something = new {id = "1", name = "name"};
Type type = something.GetType();
var properties = type.GetProperties();
foreach (var property in properties)
{
var value = property.GetGetMethod().Invoke(something, null);
Console.WriteLine(string.Format("{0}:{1}", property.Name, value));
}
Related
Hi im trying to create a class dending on data gathered from a user input. Once its chosen id like to the create the field names and the data types based on that and fill that class with data from document effectively creating a list of that Class.
Eg I create a class called Class1 and give it 3 Properties : ID , Name , Weight and define there types as int , string , int
Then I want to fill it with data Eg : (Example in json to show structure)
ID:{
1,
2,
3
},
Name:{
A,
B,
c
},
Weight:{
10,
20,
30
}
Ive looked into Reflection and codeDom which both enable for me to make the Class but i cannot work out how to write to that new classes properties.
Code for codeDom Version:
string className = "BlogPost";
var props = new Dictionary<string, Type>() {
{ "Title", typeof(string) },
{ "Text", typeof(string) },
{ "Tags", typeof(string[]) }
};
createType(className, props);
I Create The Properties and their Types
static void createType(string name, IDictionary<string, Type> props)
{
var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } });
var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "Test.Dynamic.dll", false);
parameters.GenerateExecutable = false;
var compileUnit = new CodeCompileUnit();
var ns = new CodeNamespace("Test.Dynamic");
compileUnit.Namespaces.Add(ns);
ns.Imports.Add(new CodeNamespaceImport("System"));
var classType = new CodeTypeDeclaration(name);
classType.Attributes = MemberAttributes.Public;
ns.Types.Add(classType);
foreach (var prop in props)
{
var fieldName = "_" + prop.Key;
var field = new CodeMemberField(prop.Value, fieldName);
classType.Members.Add(field);
var property = new CodeMemberProperty();
property.Attributes = MemberAttributes.Public | MemberAttributes.Final;
property.Type = new CodeTypeReference(prop.Value);
property.Name = prop.Key;
property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName)));
property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName), new CodePropertySetValueReferenceExpression()));
classType.Members.Add(property);
}
var results = csc.CompileAssemblyFromDom(parameters, compileUnit);
results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
}
This is just code i found elsewhere but if this where the code i wanted id do something like
var a = new List<BlogPost>()
and then
a."Property1" = "Title 1"
Hope this is informative
You could use reflection.
Main method to create desired object and populate its properties:
public object GenerateObject(string fullyQualifiedClassName,
Dictionary<string, object> nameToValueMap)
{
var actualObject = GetInstance(fullyQualifiedClassName);
if (actualObject == null)
return actualObject;
foreach (var prop in nameToValueMap)
{
SetPropValue(actualObject, prop.Key, prop.Value);
}
return actualObject;
}
Method to create instance of the desired class, based on fully qualified class name:
public object GetInstance(string fullyQualifiedName)
{
Type type = Type.GetType(fullyQualifiedName);
if (type != null)
return Activator.CreateInstance(type);
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
{
type = asm.GetType(fullyQualifiedName);
if (type != null)
return Activator.CreateInstance(type);
}
return null;
}
And last but not least, method to set property's value:
public bool SetPropValue<T>(T obj, string propName, object val)
{
if (string.IsNullOrEmpty(propName)) return false;
var prop = obj?.GetType()
.GetProperties()?
.FirstOrDefault(m => m.Name == propName);
if (prop != null)
{
prop.SetValue(obj, val);
return true;
}
return false;
}
Why not use dynamic object using expandoObject?
something like:
dynamic blogPost = new System.Dynamic.ExpandoObject();
blogPost.Tile = "Mary Water";
blogPost.Text= "your text here";
I am trying to update a multiple lookup value field for a list item.
I tried the following code :
List < QueryOption > options = new List < QueryOption > {
new QueryOption("$expand", "listitem")
};
//get drive item with list item
var driveItem = graphClient.Sites[IdGestDoc].Drive.Items[itemResult.Id].Request(options).GetAsync().Result;
var fieldValueSet = new FieldValueSet {
AdditionalData = new Dictionary < string,
object > {
{
"Theme_fonctionnel#odata.type",
"Collection(Edm.String)"
}, {
"Theme_fonctionnel", ThemeFonctionnel.ToArray()
} //ThemeFonctionnel is a List<string> => lookupid
}
};
await graphClient.Sites[IdGestDoc].Lists["Documents"].Items[driveItem.ListItem.Id].Fields.Request().UpdateAsync(fieldValueSet);
But this code don't work and I don't find what I am missing.
Any help will be appreciated !
To set a Lookup field, you need to set the property by passing in the property name and the addition 'LookupId':
string propertyName = "Theme_fonctionnel";
var fieldValueSet = new FieldValueSet();
var propertyValuesArray = options.ToArray();
var attributes = new Dictionary<string, object>();
//first, we need to specify the input data type
string oDataTypeInfoPropertyName = propertyName + "LookupId#odata.type";
string oDataDataType = "Collection(Edm.String)";
attributes.Add(oDataTypeInfoPropertyName, oDataDataType);
//next, we need to pass the values as an array
string newPropertyName = propertyName + "LookupId";
attributes.Add(newPropertyName, propertyValuesArray);
fieldValueSet.AdditionalData = attributes;
I am using CsvHelper to read CSV files into Dynamic C# object and I would like to iterate the List<dynamic> using foreach and get property names and values.
FileInfo file = new FileInfo("C:\\Temp\\CSVTest.csv");
List<dynamic> dynObj;
using (var reader = new StreamReader(file.FullName))
using (var csv = new CsvReader(reader))
{
dynObj = csv.GetRecords<dynamic>().ToList();
foreach (var d in dynObj)
{
var properties = d.GetType().GetProperties();
foreach (var property in properties)
{
var PropertyName = property.Name;
var PropetyValue = d.GetType().GetProperty(property.Name).GetValue(d, null);
}
}
}
var properties = d.GetType().GetProperties(); always return 0 but I can see at debug that there are properties.
the CSV file contains this data:
Id,Name,Barnd
1,one,abc
2,two,xyz
Normally, dynamic type has a System.Dynamic.ExpandoObject type object while using it. Its same as a KeyValuePairtype in C#.
Following solution will return list of keys and values from dynamic type.
using (var csv = new CsvReader(reader))
{
dynObj = csv.GetRecords<dynamic>().ToList();
foreach (var d in dynObj)
{
var obj = d as System.Dynamic.ExpandoObject;
if (obj != null)
{
var keys = obj.Select(a => a.Key).ToList();
var values = obj.Select(a => a.Value).ToList();
}
}
}
I need iterate through Dapper DynamicParameters. So, I check this answer to get value of parameter.
foreach (var paramName in parameters.ParameterNames)
{
var value = ((SqlMapper.IParameterLookup)parameters)[paramName];
}
Now, I need parameter DbType. Is it possible to get this information?
I dont know if Dapper provides a better solution for this or not, but thanks to reflection, there is nothing impossible!
var t = parameters.GetType().GetField("parameters", BindingFlags.NonPublic | BindingFlags.Instance);
if (t != null)
{
foreach (DictionaryEntry dictionaryEntry in (IDictionary)t.GetValue(parameters))
{
var dbType = (DbType)dictionaryEntry.Value?.GetType().GetProperty("DbType")?.GetValue(dictionaryEntry.Value);
}
}
You can do this! Please check the code below:
private List<DbType> GetParameterType<T>()
{
var type = typeof(T);
var properties = type.GetProperties().Select(property => property.PropertyType.Name).ToList();
var dbTypes = new List<DbType>();
foreach (var prop in properties)
{
var tryParse = Enum.TryParse<DbType>(prop, out var result);
if (tryParse)
dbTypes.Add(result);
}
return dbTypes;
}
And then, the call of this is simple: var dbTypes = GetParameterType<T>(); where T is your object
I have an object in my database, i.e. with 10 attributes.
Now I want to let the user select some of them (1 or 2 up to 10 of them) and then according by user's selection I make a list of object with the attributes selected by user
the scenario that I think about is this:
A page with check boxes that shows the attributes(columns) of that abject then user selects each of them he needs.
But here is my problem, how to make the selected check boxes run as query?
For example user selected col 1 , col 2, col 6 , col 10, how can I write a query responsible for user selection?
Example I wanna the meaningful phrase of this:
var file2 = file.Select(f => new { "attributes selected by user" }).OrderBy(what user wants)
they System.Linq.Dynamic library on Nuget is a way to go
[TestMethod]
public void StringyAndDangerous()
{
var fakePersonDbSet = new List<Person> { new Person() { FirstName = "Some", LastName = "Guy" } }.AsQueryable();
var attributes = new string[] { "FirstName", "LastName" };
var selectedFields = String.Join(",", attributes);
var exprssion = string.Format("new ({0})", selectedFields);
var result = fakePersonDbSet.Select(exprssion, attributes).Cast<dynamic>().First();
}
but you loose type safety and compile time checking. You might be better taking another approach
[TestMethod]
public void SlowerButSafer()
{
var fakePersonDbSet = new List<Person> { new Person() { FirstName = "Some", LastName = "Guy" } }.AsQueryable();
var attributes = new string[] { "FirstName", "LastName" };
var personPropertylist = CovertToKeyValuePair(fakePersonDbSet.First())
.Where(c=> attributes.Contains(c.Key))
.ToArray();
}
private IEnumerable<KeyValuePair<string, object>> CovertToKeyValuePair<T>(T #object)
{
var result = new List<KeyValuePair<string, object>>();
var properties = typeof (T).GetProperties();
foreach (var property in properties)
{
result.Add(new KeyValuePair<string, object>(property.Name, property.GetValue(#object, null)));
}
return result;
}
you'll take a performance hit both for pulling fields from the database that you don't need and for using reflection but the code will be less error prone and you won't end up with errors for trying to select columns that don't exist.
Use DynamicLinq. (link)
Extension methods:
public static T GetValue<T>(this DynamicClass dynamicObject, string propName)
{
if (dynamicObject == null)
{
throw new ArgumentNullException("dynamicObject");
}
var type = dynamicObject.GetType();
var props = type.GetProperties(BindingFlags.Public
| BindingFlags.Instance
| BindingFlags.FlattenHierarchy);
var prop = props.FirstOrDefault(property => property.Name == propName);
if (prop == null)
{
throw new InvalidOperationException("Specified property doesn't exist.");
}
return (T)prop.GetValue(dynamicObject, null);
}
public static string ToDynamicSelector(this IList<string> propNames)
{
if (!propNames.Any())
throw new ArgumentException("You need supply at least one property");
return string.Format("new({0})", string.Join(",", propNames));
}
Usage:
using System.Linq.Dynamic;
// ..
var columns = new[] { "col1", "col2", etc };
var result = context.Files.OrderBy(file => file.Id)
.Select(columns.ToDynamicSelector())
.Cast<DynamicClass>.ToList();
Result will be the collecion of DynamiClass instances wchich columns will contain selected properties.
To get single property from DynamicClass:
var columnValue = result.First().GetValue<string>("col1");
If you want to get values from IEnumerable:
var list = new List<File> { File1, File2, etc.. };
var result = list.AsQueryable().Select( /* the same as above */);