Good Day,
Lets say this is my class:
public class MyClass {
public bool boolProp { get; set; }
public string stringProp { get; set; }
}
this is my IDictionary:
IDictionary<string, string> myDict=
new IDictionary<string, string>();
myDict.Add("boolProp", "true");
myDict.Add("stringProp", "teststring");
So I want to update my class properties using Reflection where my dictionary keys matches the name of property then set its value by creating a method, how is that?
Method parameter should be like this:
public void UpdateProperties(IDictionary<string, string> myDict) {
Thanks
With GetProperty method:
IDictionary<string, string> myDict = new Dictionary<string, string>();
myDict.Add("boolProp", "true");
myDict.Add("stringProp", "teststring");
var s = new MyClass();
var t = s.GetType();
foreach (var values in myDict)
{
var p = t.GetProperty(values.Key, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
var c = TypeDescriptor.GetConverter(p.PropertyType);
var convertedValue = c.ConvertFromInvariantString(values.Value);
p.SetValue(s, convertedValue);
}
Related
I have a problem fetching object from the array object that I made. It seems it didn't fetch the object see my code below:
Product Model
public class Product
{
public string Id { get; set; }
public List<ExcelName> ShortDesc { get; set; } // I want to get the object from here
}
Short Description Model
// get this object and the properties inside it.
public class ExcelName
{
public string Name { get; set; }
public string Language { get; set; }
}
My Code
private static T SetValue<T>(Dictionary<string, object> objectValues)
{
var type = typeof(T);
var objInstance = Activator.CreateInstance(type);
if (!type.IsClass) return default;
foreach (var value in objectValues)
{
if (value.Key.Contains(":Language="))
{
var propName = value.Key.Split(':')[0];
// propName is ShortDesc object
var propInfo = type.GetProperties(BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(e => e.Name.ToLower() == propName.ToLower().Replace(" ", ""));
if (propInfo == null) continue;
if (propInfo.PropertyType.IsGenericType)
{
// I want to get the type and properties from T generic using reflection instead static
var name = typeof(ExcelName);
var excelNameObjectInstance = Activator.CreateInstance(name);
foreach (var propertyInfo in name.GetProperties())
{
propertyInfo.SetValue(excelNameObjectInstance, value.Value, null);
}
// add excelNameObjectInstance object to the list in ShortDesc
}
}
}
}
How to fetch the object from the list of ShortDesc to get the ExcelName objects.
I'm not quite sure what you're trying to do, but it seems like you want a function that instantiates a T and sets its properties according to a dictionary.
Half your code doesn't make sense to me, but my guess is correct, you shouldn't need anything more complicated than this:
private static T SetValue<T>(Dictionary<string, object> objectValues) where T : class, new()
{
var type = typeof(T);
var instance = new T();
foreach (var entry in objectValues)
{
type.GetProperty(entry.Key).SetValue(instance, entry.Value);
}
return instance;
}
If you don't expect the keys to be an exact match for property names, you can introduce a normalization function:
private static string Normalize(string input)
{
return input.ToLower().Replace(" ", "");
}
private static T SetValue<T>(Dictionary<string, object> objectValues) where T : class, new()
{
var type = typeof(T);
var instance = new T();
foreach (var entry in objectValues)
{
var prop = type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.First( x => Normalize(x.Name) == Normalize(entry.Key) );
prop.SetValue(instance, entry.Value);
}
return instance;
}
I want to use a dictionary to replace words in a HTML template between two $ signs. I've got it working when I add the keys myself, but I want to be able to replace using reflection, so that if I give this project to someone else, then they only need to change the class and the template keys.
I've got the Class with some properties. I populate this class and then try to populate the dictionary.
public class FeedMessageValue
{
public string Username { get; set; }
public string SubscriptionID { get; set; }
public DateTime MessageTime { get; set; }
}
public class FeedMessageData : IMailData
{
private FeedMessageValue feedMessageValue;
public FeedMessageData(string username, string subscriptionID, DateTime messageTime)
{
this.feedMessageValue = new FeedMessageValue
{
Username = username
, SubscriptionID = subscriptionID
, MessageTime = messageTime
};
PropertyInfo[] infos = this.feedMessageValue.GetType().GetProperties();
foreach (PropertyInfo info in infos)
{
this.getMergeValues().Add(info.Name, info.GetValue(this.feedMessageValue, null).ToString());
}
}
public Dictionary<string, string> getMergeValues()
{
return new Dictionary<string, string>();
}
}
This runs through the Email Generator:
public interface IMailData
{
Dictionary<string, string> getMergeValues();
}
public interface IEmailGenerator
{
MailMessage generateEmail(IMailData mailData, string htmlTemplate, string textTemplate);
}
public class EmailGenerator : IEmailGenerator, IRegisterInIoC
{
// Setup the rules
static readonly Regex emailRegex = new Regex(#"\$([\w\-\,\.]+)\$", RegexOptions.Compiled);
private string mergeTemplate(string template, IReadOnlyDictionary<string, string> values)
{
string emailTextData = emailRegex.Replace(template, match => values[match.Groups[1].Value]);
return emailTextData;
}
public MailMessage generateEmail(IMailData mailData, string htmlTemplate, string textTemplate)
{
// MailMessage
}
}
So, in theory one of the dictionary keyvalue pairs should now be {"Username", username}, but using my code to replace for $Username$, then it throws a bug with that there is no key for Username.
The problem is with getMergeValues(). You're creating a new instance of Dictinary<string, string> each time. Change that method to a property, or have it return a field.
As it is right now, when you're looping through the PropertyInfo[], you create a new instance of Dictionary<string, string>, add { "Username", username }, then don't do anything with the reference to the Dictionary<string, string> you created. Then on the next iteration of the loop you create a second Dictionary<string, string> this one with, perhaps, { SubscriptionId, subscriptionID }, and don't do anything with the reference. And so on and so on.
public class FeedMessageData : IMailData
{
private FeedMessageValue feedMessageValue;
public FeedMessageData(string username, string subscriptionID, DateTime messageTime)
{
MergeData = new Dictionary<string, string>();
this.feedMessageValue = new FeedMessageValue
{
Username = username
, SubscriptionID = subscriptionID
, MessageTime = messageTime
};
PropertyInfo[] infos = this.feedMessageValue.GetType().GetProperties();
foreach (PropertyInfo info in infos)
{
MergeData.Add(info.Name, info.GetValue(this.feedMessageValue, null).ToString());
}
}
public Dictionary<string, string> MergeData { get; }
}
It's quite hard for me to explain this, but I will give it a go.
Objective:
Create a LINQ query that will return a dictionary of data. However it must be a dictionary of the model which I am using.
View Model:
public class ValueBySupplierAndClaimTypeViewModel : ReportViewModel
{
public IQueryable<ValueBySupplierAndClaimTypeModel> ReportData {get; set; }
public TotalValueBySupplierAndClaimTypeModel ReportTotalData { get; set; }
public Dictionary<string, decimal> DictionaryData { get; set; }
public string output { get; set; }
}
Interface:
Dictionary<string, decimal> DictData;
TotalValueBySupplierAndClaimTypeModel GetTotalValueBySupplierAndClaimType(
int ClientID, int ReviewPeriodID, int StatusCategoryID);
SQL Repository:
public TotalValueBySupplierAndClaimTypeModel GetTotalValueBySupplierAndClaimType(int ClientID, int ReviewPeriodID, int StatusCategoryID)
{
var rt =
this.GetValueBySupplierAndClaimType(ClientID, ReviewPeriodID, StatusCategoryID);
TotalValueBySupplierAndClaimTypeModel x = new TotalValueBySupplierAndClaimTypeModel()
{
NormalTotal = rt.Sum(c=>c.Normal) ?? 0,
QueryTotal = rt.Sum( c => c.Query) ?? 0,
StrongTotal = rt.Sum( c => c.Strong) ?? 0
};
return x;
}
I'm really not sure how to do this. Can anybody help?
I have this function that converts an object to a dictionary. It gets all the properties of the class, as the dictionary's keys. May be you can modify it to meet your needs:
public Dictionary<string, object> ConvertClassToDict(object classToConvert)
{
Dictionary<string, object> result = new Dictionary<string, object>();
PropertyInfo[] properties = classToConvert.GetType().GetProperties();
List<string> propertiesNames = properties.Select(p => p.Name).ToList();
foreach (var propName in propertiesNames)
{
PropertyInfo property = properties.First(srcProp => srcProp.Name == propName);
var value = property.GetValue(classToConvert, null);
result.Add(propName, value);
}
return result;
}
The argument classToConvert, is just an instance of any class.
Similar to #lukiller's answer, but with LINQ:
public Dictionary<string, object> MapToDictionary(object instance)
{
if(instance == null) return null;
return instance.GetType()
.GetProperties()
.ToDictionary(p => p.Name,
p => p.GetValue(instance));
}
For example, let's suppose we have the following class:
public class User
{
public string Username { get; set; }
public string Password { get; set; }
}
We can print it like this (one line):
MapToDictionary(new User()
{
Username = "mcicero",
Password = "abc123"
}).ToList().ForEach(i => Console.WriteLine("{0}: {1}", i.Key, i.Value));
This prints out:
Username: mcicero
Password: abc123
What does
public object this[string name]
do
class ObjectWithProperties
{
Dictionary<string, object> properties = new Dictionary<string, object>();
public object this[string name]
{
get
{
if (properties.ContainsKey(name))
{
return properties[name];
}
return null;
}
set
{
properties[name] = value;
}
}
}
You will be able to reference the values in your dictionary directly from your object using indexes (ie, no property name)
In your case it would be
var foo = new ObjectWithProperties();
foo["bar"] = 1;
foo["kwyjibo"] = "Hello world!"
// And you can retrieve them in the same manner...
var x = foo["bar"]; // returns 1
MSDN guide: http://msdn.microsoft.com/en-gb/library/2549tw02.aspx
Basic tutorial: http://www.tutorialspoint.com/csharp/csharp_indexers.htm
Edit to answer question in comment:
This is equivalent to doing something like the following:
class ObjectWithProperties
{
public Dictionary<string, object> Properties { get; set; }
public ObjectWithProperties()
{
Properties = new Dictionary<string, object>();
}
}
// instantiate in your other class / app / whatever
var objWithProperties = new ObjectWithProperties();
// set
objWithProperties.Properties["foo"] = "bar";
// get
var myFooObj = objWithProperties.Properties["foo"]; // myFooObj = "bar"
i have the following JSON in my application.
string json = #"{""dest"":[ { ""mode"": ""1"", ""test"":""test1,test,test2""},{ ""mode"": ""2"", ""test"": ""test3"" }]}";
To get the value of dest I m using the following method.
var json_serializer = new JavaScriptSerializer();
Dictionary<string, object> dictionary = json_serializer.Deserialize<Dictionary<string, object>>(json);
public Dictionary<string, object> GetObject(Dictionary<string, object> view, string name)
{
Dictionary<string, object> result = new Dictionary<string, object>();
object value = null;
foreach (KeyValuePair<string, object> pair in view)
{
Type type = pair.Value.GetType();
if (pair.Key == name)
{
**Dictionary<string, object> child = (System.Collections.Generic.Dictionary<string, object>)pair.Value;**
result = GetObject(child, name);
if (result != null)
{
break;
}
}
else
{
}
}
return result;
}
I m getting error in the line Dictionary child = (System.Collections.Generic.Dictionary)pair.Value;.
The error says "Unable to cast object of type 'System.Collections.ArrayList' to type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]'."
Anyone knows how to fix this?
Assuming tset in your array is a typo and it actually is test, you can use concerete classes ....
string json = #"{""dest"":[ { ""mode"": ""1"", ""test"":""test1,test,test2""},{ ""mode"": ""2"", ""test"": ""test3"" }]}";
var obj = new JavaScriptSerializer().Deserialize<MyObject>(json);
public class Dest
{
public string mode { get; set; }
public string test { get; set; }
}
public class MyObject
{
public List<Dest> dest { get; set; }
}