15

I'm trying to use reflection (ultimately on unknown at compile time) object which include struct. I've got as far as TypedReference.MakeTypedReference but I've hit a wall.

Here's my Class and Struct

public class MyObject
{
    public int Id;
    public Money Amount; 
}

public struct Money
{
    public int Vaule;
    public string Code;
}

And here is how I am trying to set "Code" of "Amount" in MyObject using reflection. As I mention above, I'm looking for a solution which does not know about these types at compile time (that would be too easy!)

Here's the code I have so far (I've used [0], [1] to make the code simpler)

var obj = new MyObject() { Id = 1 };
obj.Amount.Vaule = 10;
obj.Amount.Code = "ABC";

FieldInfo[] objFields = obj.GetType().GetFields();
FieldInfo[] moneyFields = objFields[1].GetValue(obj).GetType().GetFields();

List<FieldInfo> fields = new List<FieldInfo>() { objFields[1] };
fields.AddRange( moneyFields );

TypedReference typeRef = TypedReference.MakeTypedReference( 
                           objFields[1].GetValue( obj ), fields.ToArray() );

moneyFields[1].SetValueDirect( typeRef, "XXX" );

The TypedReference.MakeTypedReference blows up with; "FieldInfo does not match the target Type." Likewise if I just pass objFields[1]. And if pass just moneyFields I get "TypedReferences cannot be redefined as primitives."

Why? Let's say I'm creating Random test fixtures and want to populate class fields with random data :)

Ian Quigley
  • 500
  • 1
  • 6
  • 22
  • Are you saying that at compile time you don't know if its using MyObject or Money, so it needs to detect which object is being used and read/write the property accordingly? – Matthew Layton Nov 09 '12 at 12:35
  • How about using AutoFixture and getting out of the reflection Rube Goldberg machines business? (Though wrt `struct`, see http://stackoverflow.com/questions/12930963/creating-a-struct-with-autofixture-throws-no-public-constructor-error) (have not run into any need for `MakeTypedReference` professionally and have to say something as I happened to actually be watching the tweet stream for once, sry!) – Ruben Bartelink Nov 09 '12 at 12:44
  • @RubenBartelink - the "why" was simply for reference. Would defo' use AutoFixture if that was what I was doing. – Ian Quigley Nov 09 '12 at 13:12
  • @activwerx It's purely using reflection on unknown objects and structs. Marc's answer below is the perfect answer for this situation. – Ian Quigley Nov 09 '12 at 13:13
  • @IanQuigley I'm guessing I definitely wouldn't like to work on the real problem... – Ruben Bartelink Nov 09 '12 at 13:17
  • @RubenBartelink the whole thing was a BAD idea, and am following the advice of Marc Gravell and many others. Poco's all the way :) I only dug into it and posted here because I wanted to know the answer academically. – Ian Quigley Nov 09 '12 at 13:22
  • Possible duplicate of [Is there a way to set properties on struct instances using reflection?](https://stackoverflow.com/questions/6280506/is-there-a-way-to-set-properties-on-struct-instances-using-reflection) – StayOnTarget May 02 '18 at 19:24

1 Answers1

21

Frankly, there's no need whatsoever for TypedReference here - just a boxed struct should work fine:

    var amountField = obj.GetType().GetField("Amount");
    object money = amountField.GetValue(obj);
    var codeField = money.GetType().GetField("Code");
    codeField.SetValue(money, "XXX");
    amountField.SetValue(obj, money);

However! I will advise you of a few things:

  • public fields instead of properties are not usually a good idea; that will often bite you later
  • mutable structs (i.e. structs that can be changed after creation) are almost never a good idea, and will bite even more often, and bite harder
  • combining mutable structs and public fields compounds it, but making it very problematic to change later
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900