Need help calling the generic method

Need help calling the generic method.

We have a method 'DeserializeToModel' that takes in [a string (an encoded Json) and a reference type] and deserialized it into the reference type model.

public class DeserializedJsonToModel 
  {
      public T DeserializeToModel<T>(string encodedJson, T model) where T : class, new()
      {
          DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings();
          settings.UseSimpleDictionaryFormat = true; //This is needed to read in the dictionary in HospitalAffiliations class

          T deserializedModel = new T();
          MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(encodedJson));
          DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedModel.GetType());
          deserializedModel = ser.ReadObject(ms) as T;
          ms.Close();
          return deserializedModel;
      }
  }

Open in new window

 
 The problem is calling the generic method on line 49 in this unit test: 'SuperHero' is a type, which is not valid in the given context      
'SuperHero' is a type, which is not valid in the given context
Here's the test class:
    [TestClass]
    public class DeserializedJsonToModelTest
    {
        private SuperHero hero;
        private DeserializedJsonToModel deserializer;

        [TestInitialize]
        public void Setup()
        {
            hero = new SuperHero()
            {
                FirstName = "Barry",
                LastName = "Allen",
                HeroName = "Flash",
                DayJob = "Investigator"
            };

            deserializer = new DeserializedJsonToModel();
        }

        [TestMethod]
        [TestCategory("Unit"), TestCategory("DeserializedJsonToModel")]
        public void DeserializedJsonToModel_Instantiated_Success()
        {
            //Arrange

            //Act

            //Assert  
            Assert.IsNotNull(deserializer);
            Assert.IsInstanceOfType(deserializer, typeof(DeserializedJsonToModel));
        }

        [TestMethod]
        [TestCategory("Unit"), TestCategory("DeserializedJsonToModel")]
        public void DeserializedJsonToModel_SuperHeroEncodedAsJson_SuperHeroModelIsReturned()
        {
            //Arrange
            string encodedJson = Json.Encode(hero);

            //Act
            var sut = deserializer.DeserializeToModel<SuperHero>(encodedJson, SuperHero);

            //Assert            
            Assert.IsInstanceOfType(sut, typeof(SuperHero));
        }
    }
    public class SuperHero
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string HeroName { get; set; }
        public string DayJob { get; set; }
    }

Open in new window

Any ideas?

TIA!
LVL 5
allanau20Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

allanau20Author Commented:
Was so close; thanks Paul for the quick reply!
0
allanau20Author Commented:
I cannot see your comment anymnore??
0
Paul JacksonSoftware EngineerCommented:
I was rethinking the code, what did you end up with ?

public class DeserializedJsonToModel 
  {
      public T DeserializeToModel<T>(string encodedJson) where T : class, new()
      {
          DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings();
          settings.UseSimpleDictionaryFormat = true; //This is needed to read in the dictionary in HospitalAffiliations class

          T deserializedModel = new T();
          MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(encodedJson));
          DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedModel.GetType());
          deserializedModel = ser.ReadObject(ms) as T;
          ms.Close();
          return deserializedModel;
      }
  }

Open in new window



 //Act
            var sut = deserializer.DeserializeToModel<SuperHero>(encodedJson);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
allanau20Author Commented:
you were correct that the reference type does not need to be passed. So, on the method definition I removed the 'T model' and it worked. Thanks again Paul!
0
Paul JacksonSoftware EngineerCommented:
No problem. glad I could help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.