Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6765113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:40:26+00:00 2026-05-26T14:40:26+00:00

I ran into a problem today when trying to set a field using FieldInfo.SetValue()

  • 0

I ran into a problem today when trying to set a field using FieldInfo.SetValue() passing a DynamicObject as the second argument. In my case, the field is a Guid and the DynamicObject should be able to convert itself to a one (using TryConvert) but it fails with an ArgumentException.

Some code that shows the problem:

// Simple impl of a DynamicObject to prove point
public class MyDynamicObj : DynamicObject
{
    public override bool TryConvert(ConvertBinder binder, out object result)
    {
        result = null;
        // Support converting this to a Guid
        if (binder.Type == typeof(Guid))
        {
            result = Guid.NewGuid();
            return true;
        }
        return false;
    }
}

public class Test
{
    public Guid MyField;
}

class Program
{
    static void Main(string[] args)
    {
        dynamic myObj = new MyDynamicObj();

        // This conversion works just fine
        Guid guid = myObj;

        var test = new Test();
        var testField = typeof(Test).GetField("MyField");

        // This, however, fails with:
        // System.ArgumentException
        //   Object of type 'ConsoleApplication1.MyDynamicObj' cannot be converted to type 'System.Guid'.
        testField.SetValue(test, myObj);
    }
}

I’m not very familiar with the whole dynamicness of C# 4, but this felt to me like something that should work.. What am I doing wrong? Is there another way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:40:27+00:00Added an answer on May 26, 2026 at 2:40 pm

    No, this shouldn’t work – because the dynamic portion ends where your code ends. The compiler is calling a method with a signature of

    void SetValue(Object obj, Object value)
    

    That method call is dynamic, but it’s just going to end up passing in a reference to the instance of MyDynamicObj. The call is resolved at execution time, but nothing in SetValue knows anything about the dynamic nature of the object whose reference you’re passing in.

    Basically you need to perform the dynamic part (the conversion in this case) in your code – the bit that involves the C# 4 compiler doing all its tricks. You’ve got to perform that conversion, and then you can call SetField.

    To put it another way – it’s a bit like calling SetField with a field of type XName, but passing in a string. Yes, there’s a conversion from string to XName, but it’s not SetField‘s job to work that out. That’s the compiler’s job.

    Now, you can get this to work by making the compiler do some of the work, but you still need to do some with reflection:

    static void Main(string[] args)
    {
        dynamic myObj = new MyDynamicObj();
    
        var test = new Test();
        var testField = typeof(Test).GetField("MyField");
    
        var method = typeof(Program)
            .GetMethod("Convert", BindingFlags.Static | BindingFlags.NonPublic);
        method = method.MakeGenericMethod(testField.FieldType);
    
        object converted = method.Invoke(null, new object[] {myObj});
        testField.SetValue(test, converted);
    }
    
    static T Convert<T>(dynamic input)
    {
        return input;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today, I ran into this weird problem with a user using Mac OS X.
Ran into this problem today, posting in case someone else has the same issue.
I ran into a huge problem with my AIR project today (I'm using new
I ran into an interesting problem at work today. I got a request to
Today I ran into the following problem with NUnit. I have a class, that
I've ran into a problem while trying to test following IRepository based on NHibernate:
I recently ran into a problem with a COM object that was using a
Ran into a strange problem in PHP today and I'm wondering if someone can
I ran into a problem today at work wherein I have a BindingGroup that
I ran into a little problem today when I was creating a really quick

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.