开发者

Why is casting to a type in this way not valid syntax?

The compiler complains on "as typeOfState" saying that typeOfState could not be found. Why is this? Is there a way to express what I am trying to accomplish?

public static readonly IDictionary<Type, string> StateDictionary = new Dictionary<Type, string>
{
    {typeof(SerializableDictionary<string, RadTabSetting>), "TabStates"},
    {typeof(SerializableDictionary<string, RadPaneSetting>), "PaneStates"},
    {typeof(SerializableDictionary<string, RadDockSetting>), "DockStates"},
    {typeof(SerializableDictionary<string, RadDockZoneSetting>), "DockZoneStates"},
    {typeof(SerializableDictionary<string, RadSplitterSetting>), "SplitterStates"},
    {typeof(SerializableDictionary<string, RadSplitBarSetting>), "SplitBarStates"},
    {typeof(SerializableDictionary<string, RadPageViewSetting>), "RadPageViewStates"},
    {typeof(GlobalSettings), "GlobalSettings"},
};

foreach (var item in StateManager.StateDictionary)
{
    string stateName = item.Value;
    Type typeOfState = item.Key;

    object stateSession = SessionRepository.Instance.GetSession(stateName) as typeOfState;

    dataToSave.Add(stateName, stateSession);
}

Cheers

EDIT: Okay. Understood that cannot use a variable as a type, even if the variable is of type "Type"

What are my options? Here's full source:

[WebMethod]
public static bool Export()
{
    bool successful = false;

    try
    {
        HttpContext.Current.Request.ContentType = "text/xml";
        HttpContext.Current.Response.Clear();

        SerializableDictionary<string, object> dataToSave = new SerializableDictionary<string, object>();

        foreach (var item in StateManager.StateDictionary)
        {
            string stateName = item.Value;
            Type typeOfState = item.Key;

            object stateSession = SessionRepository.Instance.GetSession(stateName);

            dataToSave.Add(stateName, stateSession);
        }

        XmlSerializer serializer = new XmlSerializer(dataToSave.GetType());
        serializer.Serialize(HttpContext.Current.Response.OutputStream, dataToSave);

        successful = true;
    }
 开发者_JAVA技巧   catch (Exception exception)
    {
        _logger.ErrorFormat("Unable to serialize session. Reason: {0}", exception.Message);
    }

    return successful;
}


You cannot use a Type object with the as keyword. Check the documentation for its proper usage.

To accomplish what I think you're trying to do, perhaps look into Convert.ChangeType [MSDN].


typeOfState is an object of type Type, it is not a Type. If you want to do what you are trying, you need to use Generics in some way.


Because typeOfState is a variable rather than a type. The as operator must be passed a type that is known at compile time.


The syntax is expr as T, where T refers to the name of a reference-type (e.g. classname). T is not a general expression -- that is, it cannot be a variable name or a value representing a type (even if it is a Type object).

The compiler error message should indicate about as such.

Happy coding.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜