How to cast to a generic type using reflection? [duplicate]












3
















This question already has an answer here:




  • Convert variable to type only known at run-time?

    2 answers



  • Casting generic type instances created using Reflection

    1 answer




I can create a cast function generically like this:



public IEnumerable<T> Convert<IEnumerable<T>>(object input) {
return (IEnumerable<T>) input; // this works
}


But say, I don't have T, but I have Type. How do I get this to work?:



// object is a List<int>, Type is typeof(int)
public object Convert(object input, Type type) {
return IEnumerable<Type> input; // this does not work
}









share|improve this question















marked as duplicate by Robert Harvey c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 4:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Casting is a compile-time operation; reflection is for run-time analysis. It's really not clear what you're trying to do or why. Can you explain more?

    – Eric Lippert
    Nov 23 '18 at 2:48











  • ok, i see - i wonder if adding the word enum into the title might be of use? eg How to cast to a generic enum type using reflection?

    – JohnB
    Nov 23 '18 at 2:49











  • "object is a List<int>, Type is typeof(int)" ??? What exactly do you expect to happen in converting a List to an int other than an error?

    – TnTinMn
    Nov 23 '18 at 4:12











  • Well, if implemented correctly, I expect it not to error:). Sorry, I'm trying to write pseudo code here

    – pixelbits
    Nov 23 '18 at 4:14


















3
















This question already has an answer here:




  • Convert variable to type only known at run-time?

    2 answers



  • Casting generic type instances created using Reflection

    1 answer




I can create a cast function generically like this:



public IEnumerable<T> Convert<IEnumerable<T>>(object input) {
return (IEnumerable<T>) input; // this works
}


But say, I don't have T, but I have Type. How do I get this to work?:



// object is a List<int>, Type is typeof(int)
public object Convert(object input, Type type) {
return IEnumerable<Type> input; // this does not work
}









share|improve this question















marked as duplicate by Robert Harvey c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 4:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Casting is a compile-time operation; reflection is for run-time analysis. It's really not clear what you're trying to do or why. Can you explain more?

    – Eric Lippert
    Nov 23 '18 at 2:48











  • ok, i see - i wonder if adding the word enum into the title might be of use? eg How to cast to a generic enum type using reflection?

    – JohnB
    Nov 23 '18 at 2:49











  • "object is a List<int>, Type is typeof(int)" ??? What exactly do you expect to happen in converting a List to an int other than an error?

    – TnTinMn
    Nov 23 '18 at 4:12











  • Well, if implemented correctly, I expect it not to error:). Sorry, I'm trying to write pseudo code here

    – pixelbits
    Nov 23 '18 at 4:14
















3












3








3









This question already has an answer here:




  • Convert variable to type only known at run-time?

    2 answers



  • Casting generic type instances created using Reflection

    1 answer




I can create a cast function generically like this:



public IEnumerable<T> Convert<IEnumerable<T>>(object input) {
return (IEnumerable<T>) input; // this works
}


But say, I don't have T, but I have Type. How do I get this to work?:



// object is a List<int>, Type is typeof(int)
public object Convert(object input, Type type) {
return IEnumerable<Type> input; // this does not work
}









share|improve this question

















This question already has an answer here:




  • Convert variable to type only known at run-time?

    2 answers



  • Casting generic type instances created using Reflection

    1 answer




I can create a cast function generically like this:



public IEnumerable<T> Convert<IEnumerable<T>>(object input) {
return (IEnumerable<T>) input; // this works
}


But say, I don't have T, but I have Type. How do I get this to work?:



// object is a List<int>, Type is typeof(int)
public object Convert(object input, Type type) {
return IEnumerable<Type> input; // this does not work
}




This question already has an answer here:




  • Convert variable to type only known at run-time?

    2 answers



  • Casting generic type instances created using Reflection

    1 answer








c# reflection autofac






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 4:18







pixelbits

















asked Nov 23 '18 at 2:36









pixelbitspixelbits

40k1065102




40k1065102




marked as duplicate by Robert Harvey c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 4:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Robert Harvey c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 4:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Casting is a compile-time operation; reflection is for run-time analysis. It's really not clear what you're trying to do or why. Can you explain more?

    – Eric Lippert
    Nov 23 '18 at 2:48











  • ok, i see - i wonder if adding the word enum into the title might be of use? eg How to cast to a generic enum type using reflection?

    – JohnB
    Nov 23 '18 at 2:49











  • "object is a List<int>, Type is typeof(int)" ??? What exactly do you expect to happen in converting a List to an int other than an error?

    – TnTinMn
    Nov 23 '18 at 4:12











  • Well, if implemented correctly, I expect it not to error:). Sorry, I'm trying to write pseudo code here

    – pixelbits
    Nov 23 '18 at 4:14





















  • Casting is a compile-time operation; reflection is for run-time analysis. It's really not clear what you're trying to do or why. Can you explain more?

    – Eric Lippert
    Nov 23 '18 at 2:48











  • ok, i see - i wonder if adding the word enum into the title might be of use? eg How to cast to a generic enum type using reflection?

    – JohnB
    Nov 23 '18 at 2:49











  • "object is a List<int>, Type is typeof(int)" ??? What exactly do you expect to happen in converting a List to an int other than an error?

    – TnTinMn
    Nov 23 '18 at 4:12











  • Well, if implemented correctly, I expect it not to error:). Sorry, I'm trying to write pseudo code here

    – pixelbits
    Nov 23 '18 at 4:14



















Casting is a compile-time operation; reflection is for run-time analysis. It's really not clear what you're trying to do or why. Can you explain more?

– Eric Lippert
Nov 23 '18 at 2:48





Casting is a compile-time operation; reflection is for run-time analysis. It's really not clear what you're trying to do or why. Can you explain more?

– Eric Lippert
Nov 23 '18 at 2:48













ok, i see - i wonder if adding the word enum into the title might be of use? eg How to cast to a generic enum type using reflection?

– JohnB
Nov 23 '18 at 2:49





ok, i see - i wonder if adding the word enum into the title might be of use? eg How to cast to a generic enum type using reflection?

– JohnB
Nov 23 '18 at 2:49













"object is a List<int>, Type is typeof(int)" ??? What exactly do you expect to happen in converting a List to an int other than an error?

– TnTinMn
Nov 23 '18 at 4:12





"object is a List<int>, Type is typeof(int)" ??? What exactly do you expect to happen in converting a List to an int other than an error?

– TnTinMn
Nov 23 '18 at 4:12













Well, if implemented correctly, I expect it not to error:). Sorry, I'm trying to write pseudo code here

– pixelbits
Nov 23 '18 at 4:14







Well, if implemented correctly, I expect it not to error:). Sorry, I'm trying to write pseudo code here

– pixelbits
Nov 23 '18 at 4:14














0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'