locked
Please help with generic types RRS feed

  • Question

  • probably something for you guys.

    Basically I got a method that deserialize an XML file, my problem is that I want to use this method for different XML files and place them in the appropriate class.so I got 2 classes.

    1) MainSettings class  = mainSettings.xml file

    2) MapSettings class = mapSettings.xml file

    I declared the method as a generic because I send in the constructor the different type 1) MainSettings  2) MapSettings

    I call the method this way

    MainSettings _MainSettings = new MainSettings();

    initialize("mySettings.xml", ApplicationData.Current.LocalFolder, _MainSettings);

    here is the deserialization method

    //Read the XML file and put in the Setting class public async void initialize<T>(string fileName, StorageFolder folder, T tSettingsClass) { try { var statesFile = await folder.GetFileAsync(fileName); //Deserialize xml data to object XmlSerializer xs = new XmlSerializer(tSettingsClass.GetType()); using (Stream stream = await statesFile.OpenStreamForReadAsync()) { //problem here. instead of_MainSettings and MainSettings

    //I want to use the generic type clsss

    _MainSettings = xs.Deserialize(stream) as MainSettings; } } catch (FileNotFoundException) { //CreateTheSettingsFile(); } }


    Hope I explained myself correctly.

    Thank you

    Saturday, September 28, 2013 9:28 PM

Answers

  • public async Task<T> GetSettings<T>(string fileName, StorageFolder folder) where T : class
            {
    
                T settings = default(T);
                try
                {
                    var statesFile = await folder.GetFileAsync(fileName);
    
                    XmlSerializer xs = new XmlSerializer(typeof(T));
                    using (Stream stream = await statesFile.OpenStreamForReadAsync())
                    {
                        settings = xs.Deserialize(stream) as T;
                    }
                }
                catch (FileNotFoundException)
                {
                    //CreateTheSettingsFile();
                }
                return settings;
            }
    Also, you can change return type to "void" and set settings in this method. But in this case you need to check what type you initialize (if(settings) is MapSettings) etc). I recommend you to return settings from method, not initialize in inside it.
    • Marked as answer by Avichai-Rebibo Monday, September 30, 2013 8:56 PM
    Sunday, September 29, 2013 6:10 AM

All replies

  • public async Task<T> GetSettings<T>(string fileName, StorageFolder folder) where T : class
            {
    
                T settings = default(T);
                try
                {
                    var statesFile = await folder.GetFileAsync(fileName);
    
                    XmlSerializer xs = new XmlSerializer(typeof(T));
                    using (Stream stream = await statesFile.OpenStreamForReadAsync())
                    {
                        settings = xs.Deserialize(stream) as T;
                    }
                }
                catch (FileNotFoundException)
                {
                    //CreateTheSettingsFile();
                }
                return settings;
            }
    Also, you can change return type to "void" and set settings in this method. But in this case you need to check what type you initialize (if(settings) is MapSettings) etc). I recommend you to return settings from method, not initialize in inside it.
    • Marked as answer by Avichai-Rebibo Monday, September 30, 2013 8:56 PM
    Sunday, September 29, 2013 6:10 AM
  • That worked great.

    Here is some reading Material about Constraints on type Parameters

    http://msdn.microsoft.com/en-us/library/d5x73970.aspx

    Monday, September 30, 2013 8:56 PM