I have a situation where i want to add LinePragmas to CodeDom objects. But some code dom objects have the LinePragma property and some don't.
So I'm wondering if it's possible to use the dynamic keyword to detect if the property exists on the object (without throwing an exception) and if it does then add the pragma. Here is my current method:
public static T SetSource<T>(this T codeObject, INode sourceNode)
where T : CodeObject
{
codeObject.UserData["Node"] = sourceNode.Source;
dynamic dynamicCodeObject = codeObject;
// How can I not throw an exception here?
if (dynamicCodeObject.LinePragma != null)
{
dynamicCodeObject.LinePragma = new CodeLinePragma(
sourceNode.Source.Path.AbsoluteUri,
sourceNode.Source.StartLine);
}
return codeObject;
}
UPDATE: The solution I went with was to add an extension method called Exists(). I wrote a blog post about it here: Member Exists Dynamic C# 4.0
The jist was to create an extension method that returns an object that implements DynamicObject's TryGetMember. It uses reflection to then return true or false. Which allows you to write code like this:
object instance = new { Foo = "Hello World!" };
if (instance.Reflection().Exists().Foo)
{
string value = instance.Reflection().Call().Foo;
Console.WriteLine(value);
}
You can detect if an object has a property without having to use the dynamic features of C# 4.0 - instead using the reflection features that have been around for a while (I know at least .NET 2.0, not sure about < 2.0)
PropertyInfo info = codeObject.getType().GetProperty(
"LinePragma",
BindingFlags.Public | BindingFlags.Instance
)
If it the object does not have the property, then GetProperty() will return null. You can do similar for fields ( GetField() ) and methods ( GetMethod() ).
Not only that, but once you have the PropertyInfo, you can use it directly to do your set:
info.SetValue(codeObject, new CodeLinePragma(), null);
If you're not sure whether the property has a set method, you could take the even safer route:
MethodInfo method = info.GetSetMethod();
if(method != null)
method.Invoke(codeObject, new object[]{ new CodeLinePragma() });
This also gives you the added benefit of being a little more performant over the lookup overhead of the dynamic call (can't find a reference for that statement, so I'll just float it out there).
I suppose that doesn't answer your question directly, but rather is an alternative solution to accomplish the same goal. I haven't actually used #4.0 features yet (even though I'm a huge fan of the dynamic typing available in Ruby). It certainly not as clean/readable as the dynamic solution, but if you don't want to throw an exception it may be the way to go.
EDIT: as @arbiter points out, "This is valid only for native .net dynamic objects. This will not work for example for IDispatch."
I just spent almost an hour searching for ways to get some kind of ruby-esque "RespondTo" Method on dynamic. There certainly isn't an easy answer, but I haven't given up yet.
The point made on reflection should be the thing to try.
With dynamic, the only thing I get so far is an extension method that treats your object as dynamic. If it works, it works, if not it silently fails...
public static void Dynamight<T>(this T target, Action<dynamic> action)
{
dynamic d = target;
try
{
action(d);
}
catch (RuntimeBinderException)
{
//That was that, didn't work out
}
}
Then you can do...
string h = "Hello";
h.Dynamight(d => Console.WriteLine(d.Length)); //Prints out 5
h.Dynamight(d => d.Foo()); //Nothing happens
Update:
Since I am getting downvotes and what-have-you let me be more concise than the subtle naming of the extension method: It is dynamite (Geddit?)! Gobbling exceptions and doing nothing is bad. This is no production code, but a version 1 of a spike of a proof of concept. I keep forgetting that you can't be subtle on a multi-thousands forum like stackoverflow. Mea culpa.
18 months later ... it seems like what you really wanted is there now that it's released. It's the TryGetMember, TryGetValue, etc... actually, probably TrySetMember, specifically.
http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject_members.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With