0

Working on several RDLC, in C# VS2017, I found that I need to send specific instances of my own objects. Avoiding the struggling with a large switch, I want to know if there is some way to create an instance on the run time by the class name

I've a dummy fiddle in order to test it, but can't complete it: https://dotnetfiddle.net/eMwleG

My code until now, after a lot of test and answers from SO, from JSON Conversion to Casting types and using reflection:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Reflection;                    
public class Program
{
    public static T GetObjectAs<T>(object source, T destinationType)
   where T: class
{
     return Convert.ChangeType(source, typeof(T)) as T;
}

    public static void Main()
    {
        //From Object to string
        var nameMessage = new MessageWrapper<Name>();
        nameMessage.Message = new Name {First="Bob", Last = "Smith"};
        string serialized = JsonConvert.SerializeObject(nameMessage);

        //From String to Object
        var deserialized = JsonConvert.DeserializeObject<MessageWrapper>(serialized);
        var messageType = Type.GetType(deserialized.MessageType);

        var message = JsonConvert.DeserializeObject(Convert.ToString(deserialized.Message), messageType);

        Name myname = GetObjectAs(message, System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("Name"));

        //Name myname =  Convert.ChangeType(message,messageType) as Name;
        Console.WriteLine(myname.First);

    }
}

public class MessageWrapper<T>
{
    public string MessageType { get { return typeof(T).FullName; } }
    public T Message { get; set; }
}

public class MessageWrapper
{
    public string MessageType { get; set; }
    public object Message { get; set; }
}


public class Name
{
    public string First;
    public string Last;
}

So my question is:

How can create an specific instance of my object "Name" in run time and assign it from an object casted by a dynamic conversion?

Please don't try to cover the logic of the example, is a dummy example to do something. I can accept answers and ideas of course, with the same spirit, take in consideration I don't "know" the class called to be casted.

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
  • 1
    Are you looking for `TypeNameHandling` functionality? See [Json.net serialize/deserialize derived types?](https://stackoverflow.com/q/8513042), [Using a custom type discriminator to tell JSON.net which type of a class hierarchy to deserialize](https://stackoverflow.com/q/11099466) and maybe [JSON.Net: deserializing polymorphic types without specifying the assembly](https://stackoverflow.com/q/13598969). But see [TypeNameHandling caution in Newtonsoft Json](https://stackoverflow.com/q/39565954) for possible security risks. – dbc Jul 26 '19 at 03:48
  • Thanks for your reply, @dbc, good links that improve my question for further visitors. I came from a lot of these links but didn't know about the security risk. I thought it before anyway, but the API is a deep layer of my own app so I don't care about that because is on my control, but definitely is something to take in consideration – Leandro Bardelli Jul 26 '19 at 14:41

1 Answers1

1

To use Convert.ChangeType, the source object must implement IConvertible.

You can cast it directly, so instead of

Convert.ChangeType(source, typeof(T)) as T

you can do

(T)source or source as T.

Here is a fiddle showing the code: https://dotnetfiddle.net/dhBeMF

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
Sohaib Jundi
  • 1,576
  • 2
  • 7
  • 15