سؤال

Which method of DynamicObject do I have to override in order to get a different behavior (in the dynamic class) based on the context in which the instance is used?

Here is an example of what I am trying to accomplish:

class DynamicTest : DynamicObject
{
    public DynamicTest(string xyz)
    {
        _xyz = xyz;
    }

    private string _xyz;

    //TODO: what do I need to implement to get required behaviour?
}

class Program
{
    static void Main(string[] args)
    {
        dynamic foo = new DynamicTest("test);

        if (foo)  // treat foo as boolean
        { // jump in here when _xyz of foo has a value
            System.Console.WriteLine(foo); //treat foo as string
        }
        else
        { // jump in here when _xyz of foo is null
            System.Console.WriteLine("No Value In Object");
        }
    }
}
هل كانت مفيدة؟

المحلول 2

I think implicit operator can help you

Example code:

class DynamicTest : DynamicObject
{
    public DynamicTest(string xyz)
    {
        _xyz = xyz;
    }

    private string _xyz;

    public static implicit operator bool(DynamicTest rhs)
    {
        return rhs._xyz != null;
    }

    public static implicit operator string(DynamicTest rhs)
    {
        return rhs._xyz;

    }

    //TODO: what to override to get required behaviour
}



class Program
{
    static void Main(string[] args)
    {
        dynamic foo = new DynamicTest("test");


        if (foo)  // treat foo as boolean
        { // jump in here when _xyz of foo has a value
            System.Console.WriteLine((string)foo); //treat foo as string   //Importat: (string)foo to go operatorstring 
        }
        else
        { // jump in here when _xyz of foo is null
            System.Console.WriteLine("No Value In Object");
        }


    }
}

نصائح أخرى

I don't know why are you trying to do this and I will definitely NOT will recommend doing this, but you can override TryConvert method on DynamicObject like:

class DynamicTest : DynamicObject
{
    public DynamicTest(string xyz)
    {
        _xyz = xyz;
    }
    private string _xyz;


    public override bool TryConvert(ConvertBinder binder, out Object result)
    {
        Console.WriteLine ("TryConvert was called");
        Console.WriteLine ("Is explicit: "+binder.Explicit);
        if(binder.Type == typeof(bool))
        {
            result = true;
            return true;
        }
        else if(binder.Type == typeof(string))
        {   
            result = _xyz;
            return true;
        }

        result = null;
        return false;
    }

    public override string ToString()
    {
        return _xyz;
    }
}

Now there are some issues: ToString is required for Console.WriteLine, it doesn't try to convert if no implicit convertions exist (because WriteLine is overloaded), so it calls ToString. Implicit and explicit conversions to bool pass, but if you use foo inside if - you will get RuntimeBinderException: Cannot implicitly convert type 'DynamicTest' to 'bool'.

Examples:

dynamic foo = new DynamicTest("test:");
bool boolFoo = foo; //passes, TryConvert is called with `binder.Explicit` == false
bool boolFoo1 = (bool)foo; //passes, TryConvert is called with `binder.Explicit` == true
if(foo) //throws RuntimeBinderException
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top