Sean Anderson
Sean Anderson

Reputation: 29331

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;
    }
    catch (Exception exception)
    {
        _logger.ErrorFormat("Unable to serialize session. Reason: {0}", exception.Message);
    }

    return successful;
}

Upvotes: 1

Views: 93

Answers (4)

FishBasketGordo
FishBasketGordo

Reputation: 23142

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

Upvotes: 2

user166390
user166390

Reputation:

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.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613402

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

Upvotes: 1

Erik A. Brandstadmoen
Erik A. Brandstadmoen

Reputation: 10588

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.

Upvotes: 2

Related Questions