I think you need to first unbox the value in table. Rows0p. Name as a decimal In other words: foreach (PropertyInfo p in props) { if (p.
PropertyType == typeof(Currency)) { Currency c = (decimal)table. Rows0p. Name; p.
SetValue(this, c, null); } else { p. SetValue(this, table. Rows0p.Name, null); } } This is an issue I've seen once or twice before, so I actually decided to write a blog post about it Anybody looking for a little more explanation, feel free to give it a read.
I think you need to first unbox the value in table. Rows0p. Name as a decimal.In other words: foreach (PropertyInfo p in props) { if (p.
PropertyType == typeof(Currency)) { Currency c = (decimal)table. Rows0p.Name; p. SetValue(this, c, null); } else { p.
SetValue(this, table. Rows0p. Name, null); } } This is an issue I've seen once or twice before, so I actually decided to write a blog post about it.
Anybody looking for a little more explanation, feel free to give it a read.
This worked excellently! Thank you! – Chris Boden May 5 '10 at 21:08.
Unfortunately, these user-defined conversion operators are not used by the runtime; they are only used by the compiler at compile time. So if you take a strongly-typed decimal and assign it to a strongly-typed Currency, the compiler will insert a call to your conversion operator and everybody's happy. However, when you call SetValue as you're doing here, the runtime expects you to give it a value of the appropriate type; the runtime has no idea that this conversion operator exists, and won't ever call it.
2 Exactly. Implicit Conversions are similar to extension methods and default parameters - something the compiler does. – TomTom May 5 '10 at 19:28 Ah, I see.
I appreciate the insight, I should be able to figure out a better way to do this. Thank you :) – Chris Boden May 5 '10 at 19:32.
I assume that table is of type DataTable in your code, so the first indexer returns a DataRow, and the second one returns an object. Then PropertyInfo. SetValue also takes an object as the second argument.At no place in this code a cast happens in the code, which is why the overloaded conversion operator is not applied.
Generally speaking, it is only applied when static types are known (forgetting about dynamic in C# 4.0 for the moment). It is not applied when boxing and unboxing things.In this case, the indexer on DataRow boxes the value, and PropertyInfo. SetValue tries to unbox it to a different type - and fails.
Whilst I am not answering your problem, I think in this kind of situation it would be more appropriate to use a ORM Framework like the Entity Framework or Nbernate which will map your tables into your domain objects and handle all the conversions for you. Using something like reflection to figure out what fields to fill in a domain object is a slow way to do it.
1 Normally I'd be pretty worried about it, but it doesn't really impact it from what I can gather. It's a relatively small set of data (56 columns/properties) so the added overhead isn't a big issue. I appreciate the suggestion though.
– Chris Boden May 5 '10 at 19:30.
I assume that table is of type DataTable in your code, so the first indexer returns a DataRow, and the second one returns an object. SetValue also takes an object as the second argument. At no place in this code a cast happens in the code, which is why the overloaded conversion operator is not applied.
Generally speaking, it is only applied when static types are known (forgetting about dynamic in C# 4.0 for the moment). It is not applied when boxing and unboxing things. In this case, the indexer on DataRow boxes the value, and PropertyInfo.
SetValue tries to unbox it to a different type - and fails.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.