views:

594

answers:

5

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);
}
+6  A: 

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."

Matt
This is valid only for native .net dynamic objects. This will not work for example for IDispatch.
arbiter
@arbiter ahh, good point. Clearly demonstrating my lack of experience in actually using .NET 4.0. +1
Matt
I don't think this will work, see my answer below
zvolkov
@zvolkov I don't understand why your answer suggests this one wont work - can you elaborate?
Matt
This was what I ended up doing too. I was just hoping there would be a more clever and elegant way to do this with the dynamic keyword. Or at least that calling a non-existent property wouldn't throw an exception :(
justin.m.chase
The question wasn't that this works for any dynamic object but especially for classes in the CodeDOM namespace. The API is somewhat messy: There are no indicators which classes have LinePragma and which don't. So you're stuck with plenty of type checks or do something with reflection or indeed use dynamic.
flq
This won't work if the target class implements IDynamicObject or subclasses DynamicObject. See my answer below for details.
zvolkov
I went with a reflection based approach in the end. Here is my solution:http://justnbusiness.com/post/2009/07/02/Member-Exists-e28093-Dynamic-C-40.aspxBasically has a fluent style API with a dynamic wrapper for seeing if unknown members exist first.
justin.m.chase
+2  A: 

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.

flq
+1 Pretty cool stuff. But when does it start getting too clever?
Robert Harvey
I'm not sure about the silent failure there - sounds like setting up a scenario where a bug will disappear into a black hole of obscurity. In Ruby you still get a run time error if there is no method - *unless* the class also defines the method_missing method. And even then you have the choice to handle it as you see fit and still drop an "undefined method" error if you so choose.
Matt
The extension method only swallows a RuntimeBinderException, which is the desired behavior in this scenario. All other exceptions would pass up the call stack. I don't see the problem here.
Robert Harvey
I thought theught the "might" misspelling was a bit of a joke since the method "might" not exist?
justin.m.chase
A: 

I'm going to chime in and say static typing would avoid this problem.

This is a candidate for an abstract method with overriding.

Well the problem here is that I'm trying to access a property on CodeDom objects. If you're not familiar there are probably 50 of them and maybe half or so with the LinePragma property. Unfortunately, that property is not found on any particular shared base type or interface. So with strong typing you have to do a try and fail cast of a whole bunch of objects in order to find the right one. Very tedious.
justin.m.chase
A: 

Think about it: since the target class can provide its own implementation for the member lookup and invocation for non-existing members (by implementing IDynamicObject or subclassing DynamicObject) the only way to verify if a member exists is to invoke it and see whether the object handles it or throws an exception.

Once again, handling of non-existing members is DYNAMIC!

--EDIT--

If you control object creation, you could subclass the class and implement IDynamicObject to signal your other class that method does not exist.

It's unfair to downvote the answer if it points out the truth -- i.e. that there's no and cannot be a reliable way to check member existence in dynamic dispatch environment other than invoking the member.

zvolkov
It's not my type, I cannot add interfaces too it. :(
justin.m.chase
Also, invoking a non-existent member throws an exception. I need more of a TryInvoke type of thing.
justin.m.chase
This is all nice, but the question is about CodeDom objects, that are not dynamic. And he was just wondering if C#4 could help to discover the property. Also there's no need for UPPER CASING A WHOLE SENTENCE JUST TO TRY TO MAKE A POINT.
Jb Evain
The title was "C# 4.0, detect if a method is missing" -- which is exactly what I addressed. Valid point re: caps though, fixed.
zvolkov
+1  A: 

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

Jaykul
TryGetMember only exists on objects that inherit from DynamicObject though... casting it to (dynamic) isn't quite the same as casting it to (DynamicObject). When you cast it to dynamic and try to call members on it the it will call TryGetValue on the dynamic wrapper and throw an exception if it fails.I suppose it might possible to cast it like "(DynamicObject)(dynamic)obj" ?
justin.m.chase