2

This is what I am trying to do:

public void method(int myVal, string myOtherVal)
{
  // doing something
}

dynamic myVar = new SomeDynamicObjectImplementer();
method(myVar.IntProperty, myVar.StringProperty);

Note that my properties are also DynamicObjects. My problem is that the TryConvert method is never called and that I get a runtime error saying the method signature is invalid.

The following is working great:

string strVar = myVar.StringProperty;
int intVar = myVar.IntProperty;

And I would like to avoid

method((int)myVar.IntProperty, (string)myVar.StringProperty);

Is it possible to override something in DynamicObject to allow this? (or something else)

Thank you

3 Answers 3

2

The problem is your assumption that it will try a dynamic implicit convert on arguments of an dynamic invocation to make a method call work, this is not true.

When your arguments aren't statically typed, it will use the runtime type to find the best matching method (if the runtime type matches the static rules for implicit conversion to the argument type this will work too), since your your IntProperty,StringProperty seem to be returning a DynamicObject rather than an Int and a String or something that could statically be converter implicitly, this lookup will fail.

If SomeDynamicObjectImplementer could actually return an Int for IntProperty and a String for StringProperty your method call for without casting would actually work. It's also probably a better dynamic typing practice if you data type is based on the actually type of data rather than usage using try convert. You could add actually implicit convert methods for every possible type that you could return to that returned DynamicObject type, but that could cause strange resolution issues to depending on how much you are overloading.

However, another option to keep your dynamic implementation the same is to mix a little controlled static typing in, you can use ImpromputInterface (in nuget) to put an interface on top of a dynamic object, if you do that then the TryConvert method would be called on your returned DynamicObjects.

public interface ISomeStaticInterface{
      int IntProperty {get;}
      string StringProperty {get;}
}
...
var myVar = new SomeDynamicObjectImplementer().ActLike<ISomeStaticInterface>();
method(myVar.IntProperty, myVar.StringProperty);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I had an insight about this yesterday and you confirmed it. I'll see what I can do maybe I can Implement something on "SomeDynamicObjectImplementer" for this.
0

Instead of using myVar.IntProperty can't you just put them in variables first, like you already did, and then use then for your method?

so method(intVar , strVar); seems fine. At least more elegant than casting.

Of course, if you're already certain your object will have IntProperty and StringProperty, why not just make an actual object with those properties instead?

2 Comments

I can put them in a static variable or do a casting and it will work. I'm trying to avoid making a variable or a cast for every parameter here. For example, if I put them in a static variable, I don't have to cast and everything works. Why do I have to cast for a method call? That is my question.
Also, the whole point of using dynamic here is to not have to create a bunch of DTO's and parsing/convert code to pass data around.
0

Why are you doing the cast?

method(myVar.IntProperty, myVar.StringProperty);

should compile.

If the two properties must be the types suggested by the names then they shouldn't be dynamic.

1 Comment

It does compile, but it breaks at runtime. I'm doing the cast to force the type conversion at runtime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.