开发者

Implicit Conversion Not Working for Dynamic Type

开发者 https://www.devze.com 2023-02-16 14:40 出处:网络
I am running into a problem when trying to implicitly convert one of my dynamic types. There are two assemblies with definitions similar to the following:

I am running into a problem when trying to implicitly convert one of my dynamic types. There are two assemblies with definitions similar to the following:

Configuration.dll:

public class ConfigurationValue : DynamicObject  
{
    public ConfigurationValue(string val开发者_开发百科)
    {
        //...
    }

    //...  

    public static implicit operator string(ConfigurationValue val)  
    {  
        return val.ToString();  
    }  
}  

There is another class in this dll called Configuration with a member variable called Instance (to make the class singleton). This variable holds the ConfigurationValue instances in a dictionary and is of type dynamic. This allows me to do this following:

Server.dll:

//...

if (Configuration.Instance.SecurityLevel != "Insecure")
{
    //...
}

Assuming that SecurityLevel is in the dictionary. This if statement appears verbatim in my code and always fails with the following error:

{"Operator '!=' cannot be applied to operands of type 'System.Dynamic.DynamicObject' and 'string'"}

Previously, when these two classes were in the same assembly, this code worked fine. Can anyone tell me what I'm doing wrong here?

Thanks,

Max


Solved the problem, a little embarrassing actually, I forgot to change the container class for ConfigurationValue (e.g. the type of Configuration.Instance) from internal to public when I moved it to the new assembly, so of course the type couldn't be resolved and the implicit conversion was not found


Try

var SecurityLevel = new ConfigurationValue("Insecure");
0

精彩评论

暂无评论...
验证码 换一张
取 消