How to pass class name to DeserializeObject which i will get at runtime in string c#












0














I have to call below method which has 2 parameter



public static byte readdata(string classtype, string msg)
{
Encoding encoding = new System.Text.UTF8Encoding();
string header = $"{classtype},{Guid.NewGuid().ToString()}";

byte headerBytes = Encoding.UTF8.GetBytes(header);

var test = JsonConvert.DeserializeObject<classtype>(msg);



i want to pass classtype to method DeserializeObject which i will get at runtime.




but parameter is generic i may get ClassA or ClassB or ClassC so how can i call method and pass generic class name which i'm getting it into string value?



readdata("ClassA" , "testmessage");









share|improve this question




















  • 1




    You used the generic tag so why not a generic rather than passing a string?
    – WelcomeOverflow
    Nov 11 at 18:26






  • 2




    when the type ClassA is in scope of readdata, then it may be in scope of callers of readdata too, so why not pass the type instead of a string? If it must be a string, you can use nameof(ClassA). To obtain a type from a string, look into reflection, for example here
    – dlatikay
    Nov 11 at 18:26












  • Then according to your last comment it is a possible duplicate of Getting a System.Type from type's partial name
    – dlatikay
    Nov 11 at 18:37


















0














I have to call below method which has 2 parameter



public static byte readdata(string classtype, string msg)
{
Encoding encoding = new System.Text.UTF8Encoding();
string header = $"{classtype},{Guid.NewGuid().ToString()}";

byte headerBytes = Encoding.UTF8.GetBytes(header);

var test = JsonConvert.DeserializeObject<classtype>(msg);



i want to pass classtype to method DeserializeObject which i will get at runtime.




but parameter is generic i may get ClassA or ClassB or ClassC so how can i call method and pass generic class name which i'm getting it into string value?



readdata("ClassA" , "testmessage");









share|improve this question




















  • 1




    You used the generic tag so why not a generic rather than passing a string?
    – WelcomeOverflow
    Nov 11 at 18:26






  • 2




    when the type ClassA is in scope of readdata, then it may be in scope of callers of readdata too, so why not pass the type instead of a string? If it must be a string, you can use nameof(ClassA). To obtain a type from a string, look into reflection, for example here
    – dlatikay
    Nov 11 at 18:26












  • Then according to your last comment it is a possible duplicate of Getting a System.Type from type's partial name
    – dlatikay
    Nov 11 at 18:37
















0












0








0


1





I have to call below method which has 2 parameter



public static byte readdata(string classtype, string msg)
{
Encoding encoding = new System.Text.UTF8Encoding();
string header = $"{classtype},{Guid.NewGuid().ToString()}";

byte headerBytes = Encoding.UTF8.GetBytes(header);

var test = JsonConvert.DeserializeObject<classtype>(msg);



i want to pass classtype to method DeserializeObject which i will get at runtime.




but parameter is generic i may get ClassA or ClassB or ClassC so how can i call method and pass generic class name which i'm getting it into string value?



readdata("ClassA" , "testmessage");









share|improve this question















I have to call below method which has 2 parameter



public static byte readdata(string classtype, string msg)
{
Encoding encoding = new System.Text.UTF8Encoding();
string header = $"{classtype},{Guid.NewGuid().ToString()}";

byte headerBytes = Encoding.UTF8.GetBytes(header);

var test = JsonConvert.DeserializeObject<classtype>(msg);



i want to pass classtype to method DeserializeObject which i will get at runtime.




but parameter is generic i may get ClassA or ClassB or ClassC so how can i call method and pass generic class name which i'm getting it into string value?



readdata("ClassA" , "testmessage");






c# c#-4.0






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:38

























asked Nov 11 at 18:22









Neo

5,53137133257




5,53137133257








  • 1




    You used the generic tag so why not a generic rather than passing a string?
    – WelcomeOverflow
    Nov 11 at 18:26






  • 2




    when the type ClassA is in scope of readdata, then it may be in scope of callers of readdata too, so why not pass the type instead of a string? If it must be a string, you can use nameof(ClassA). To obtain a type from a string, look into reflection, for example here
    – dlatikay
    Nov 11 at 18:26












  • Then according to your last comment it is a possible duplicate of Getting a System.Type from type's partial name
    – dlatikay
    Nov 11 at 18:37
















  • 1




    You used the generic tag so why not a generic rather than passing a string?
    – WelcomeOverflow
    Nov 11 at 18:26






  • 2




    when the type ClassA is in scope of readdata, then it may be in scope of callers of readdata too, so why not pass the type instead of a string? If it must be a string, you can use nameof(ClassA). To obtain a type from a string, look into reflection, for example here
    – dlatikay
    Nov 11 at 18:26












  • Then according to your last comment it is a possible duplicate of Getting a System.Type from type's partial name
    – dlatikay
    Nov 11 at 18:37










1




1




You used the generic tag so why not a generic rather than passing a string?
– WelcomeOverflow
Nov 11 at 18:26




You used the generic tag so why not a generic rather than passing a string?
– WelcomeOverflow
Nov 11 at 18:26




2




2




when the type ClassA is in scope of readdata, then it may be in scope of callers of readdata too, so why not pass the type instead of a string? If it must be a string, you can use nameof(ClassA). To obtain a type from a string, look into reflection, for example here
– dlatikay
Nov 11 at 18:26






when the type ClassA is in scope of readdata, then it may be in scope of callers of readdata too, so why not pass the type instead of a string? If it must be a string, you can use nameof(ClassA). To obtain a type from a string, look into reflection, for example here
– dlatikay
Nov 11 at 18:26














Then according to your last comment it is a possible duplicate of Getting a System.Type from type's partial name
– dlatikay
Nov 11 at 18:37






Then according to your last comment it is a possible duplicate of Getting a System.Type from type's partial name
– dlatikay
Nov 11 at 18:37














1 Answer
1






active

oldest

votes


















2














Not sure what you are trying but should not this work:



readdata(nameof(ClassA), "");
readdata(nameof(ClassB), "");
...


You could use the following overload JsonConvert.DeserializeObject



var test = JsonConvert.DeserializeObject(msg, Type.GetType("namespace.qualified.ClassA"));


Assuming you are passing generic type parameters - here is what you can try:



public static void readdata(string classtype, string msg)
{
///... your stuff
}

static void SendType<T>()
{
readdata(typeof(T).Name, "");
}

static void Main(string args)
{
SendType<ClassA>();
SendType<ClassB>();
SendType<ClassC>();
}


But i am not sure if the above will still solve your problem, you should try:



public static byte readdata<T>(string msg)
{
//Your code
var test = JsonConvert.DeserializeObject<T>(msg);
}


Calling statement:



readdata<ClassA>("Some message");
readdata<ClassB>("Some message");
readdata<ClassC>("Some message");


Suggest to read the following Type.GetType(“namespace.a.b.ClassName”) returns null






share|improve this answer























  • i cannot do like this readdata<ClassA>("Some message"); because at runtime i'm getting value of class name.
    – Neo
    Nov 11 at 18:36










  • @Neo - check my edit
    – Sadique
    Nov 11 at 18:38










  • Type.GetType works for me but it need namespace.qualified.classname full is there any other way to do it without a full specified?
    – Neo
    Nov 11 at 18:47








  • 1




    @Neo - can you go to an Address if you only have the name of a city? I think you are doing something wrong.
    – Sadique
    Nov 11 at 18:48










  • got you :) thanks
    – Neo
    Nov 11 at 18:50











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251807%2fhow-to-pass-class-name-to-deserializeobject-which-i-will-get-at-runtime-in-strin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Not sure what you are trying but should not this work:



readdata(nameof(ClassA), "");
readdata(nameof(ClassB), "");
...


You could use the following overload JsonConvert.DeserializeObject



var test = JsonConvert.DeserializeObject(msg, Type.GetType("namespace.qualified.ClassA"));


Assuming you are passing generic type parameters - here is what you can try:



public static void readdata(string classtype, string msg)
{
///... your stuff
}

static void SendType<T>()
{
readdata(typeof(T).Name, "");
}

static void Main(string args)
{
SendType<ClassA>();
SendType<ClassB>();
SendType<ClassC>();
}


But i am not sure if the above will still solve your problem, you should try:



public static byte readdata<T>(string msg)
{
//Your code
var test = JsonConvert.DeserializeObject<T>(msg);
}


Calling statement:



readdata<ClassA>("Some message");
readdata<ClassB>("Some message");
readdata<ClassC>("Some message");


Suggest to read the following Type.GetType(“namespace.a.b.ClassName”) returns null






share|improve this answer























  • i cannot do like this readdata<ClassA>("Some message"); because at runtime i'm getting value of class name.
    – Neo
    Nov 11 at 18:36










  • @Neo - check my edit
    – Sadique
    Nov 11 at 18:38










  • Type.GetType works for me but it need namespace.qualified.classname full is there any other way to do it without a full specified?
    – Neo
    Nov 11 at 18:47








  • 1




    @Neo - can you go to an Address if you only have the name of a city? I think you are doing something wrong.
    – Sadique
    Nov 11 at 18:48










  • got you :) thanks
    – Neo
    Nov 11 at 18:50
















2














Not sure what you are trying but should not this work:



readdata(nameof(ClassA), "");
readdata(nameof(ClassB), "");
...


You could use the following overload JsonConvert.DeserializeObject



var test = JsonConvert.DeserializeObject(msg, Type.GetType("namespace.qualified.ClassA"));


Assuming you are passing generic type parameters - here is what you can try:



public static void readdata(string classtype, string msg)
{
///... your stuff
}

static void SendType<T>()
{
readdata(typeof(T).Name, "");
}

static void Main(string args)
{
SendType<ClassA>();
SendType<ClassB>();
SendType<ClassC>();
}


But i am not sure if the above will still solve your problem, you should try:



public static byte readdata<T>(string msg)
{
//Your code
var test = JsonConvert.DeserializeObject<T>(msg);
}


Calling statement:



readdata<ClassA>("Some message");
readdata<ClassB>("Some message");
readdata<ClassC>("Some message");


Suggest to read the following Type.GetType(“namespace.a.b.ClassName”) returns null






share|improve this answer























  • i cannot do like this readdata<ClassA>("Some message"); because at runtime i'm getting value of class name.
    – Neo
    Nov 11 at 18:36










  • @Neo - check my edit
    – Sadique
    Nov 11 at 18:38










  • Type.GetType works for me but it need namespace.qualified.classname full is there any other way to do it without a full specified?
    – Neo
    Nov 11 at 18:47








  • 1




    @Neo - can you go to an Address if you only have the name of a city? I think you are doing something wrong.
    – Sadique
    Nov 11 at 18:48










  • got you :) thanks
    – Neo
    Nov 11 at 18:50














2












2








2






Not sure what you are trying but should not this work:



readdata(nameof(ClassA), "");
readdata(nameof(ClassB), "");
...


You could use the following overload JsonConvert.DeserializeObject



var test = JsonConvert.DeserializeObject(msg, Type.GetType("namespace.qualified.ClassA"));


Assuming you are passing generic type parameters - here is what you can try:



public static void readdata(string classtype, string msg)
{
///... your stuff
}

static void SendType<T>()
{
readdata(typeof(T).Name, "");
}

static void Main(string args)
{
SendType<ClassA>();
SendType<ClassB>();
SendType<ClassC>();
}


But i am not sure if the above will still solve your problem, you should try:



public static byte readdata<T>(string msg)
{
//Your code
var test = JsonConvert.DeserializeObject<T>(msg);
}


Calling statement:



readdata<ClassA>("Some message");
readdata<ClassB>("Some message");
readdata<ClassC>("Some message");


Suggest to read the following Type.GetType(“namespace.a.b.ClassName”) returns null






share|improve this answer














Not sure what you are trying but should not this work:



readdata(nameof(ClassA), "");
readdata(nameof(ClassB), "");
...


You could use the following overload JsonConvert.DeserializeObject



var test = JsonConvert.DeserializeObject(msg, Type.GetType("namespace.qualified.ClassA"));


Assuming you are passing generic type parameters - here is what you can try:



public static void readdata(string classtype, string msg)
{
///... your stuff
}

static void SendType<T>()
{
readdata(typeof(T).Name, "");
}

static void Main(string args)
{
SendType<ClassA>();
SendType<ClassB>();
SendType<ClassC>();
}


But i am not sure if the above will still solve your problem, you should try:



public static byte readdata<T>(string msg)
{
//Your code
var test = JsonConvert.DeserializeObject<T>(msg);
}


Calling statement:



readdata<ClassA>("Some message");
readdata<ClassB>("Some message");
readdata<ClassC>("Some message");


Suggest to read the following Type.GetType(“namespace.a.b.ClassName”) returns null







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 18:37

























answered Nov 11 at 18:28









Sadique

19k44986




19k44986












  • i cannot do like this readdata<ClassA>("Some message"); because at runtime i'm getting value of class name.
    – Neo
    Nov 11 at 18:36










  • @Neo - check my edit
    – Sadique
    Nov 11 at 18:38










  • Type.GetType works for me but it need namespace.qualified.classname full is there any other way to do it without a full specified?
    – Neo
    Nov 11 at 18:47








  • 1




    @Neo - can you go to an Address if you only have the name of a city? I think you are doing something wrong.
    – Sadique
    Nov 11 at 18:48










  • got you :) thanks
    – Neo
    Nov 11 at 18:50


















  • i cannot do like this readdata<ClassA>("Some message"); because at runtime i'm getting value of class name.
    – Neo
    Nov 11 at 18:36










  • @Neo - check my edit
    – Sadique
    Nov 11 at 18:38










  • Type.GetType works for me but it need namespace.qualified.classname full is there any other way to do it without a full specified?
    – Neo
    Nov 11 at 18:47








  • 1




    @Neo - can you go to an Address if you only have the name of a city? I think you are doing something wrong.
    – Sadique
    Nov 11 at 18:48










  • got you :) thanks
    – Neo
    Nov 11 at 18:50
















i cannot do like this readdata<ClassA>("Some message"); because at runtime i'm getting value of class name.
– Neo
Nov 11 at 18:36




i cannot do like this readdata<ClassA>("Some message"); because at runtime i'm getting value of class name.
– Neo
Nov 11 at 18:36












@Neo - check my edit
– Sadique
Nov 11 at 18:38




@Neo - check my edit
– Sadique
Nov 11 at 18:38












Type.GetType works for me but it need namespace.qualified.classname full is there any other way to do it without a full specified?
– Neo
Nov 11 at 18:47






Type.GetType works for me but it need namespace.qualified.classname full is there any other way to do it without a full specified?
– Neo
Nov 11 at 18:47






1




1




@Neo - can you go to an Address if you only have the name of a city? I think you are doing something wrong.
– Sadique
Nov 11 at 18:48




@Neo - can you go to an Address if you only have the name of a city? I think you are doing something wrong.
– Sadique
Nov 11 at 18:48












got you :) thanks
– Neo
Nov 11 at 18:50




got you :) thanks
– Neo
Nov 11 at 18:50


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251807%2fhow-to-pass-class-name-to-deserializeobject-which-i-will-get-at-runtime-in-strin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation