How to cast to a generic type using reflection? [duplicate]
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
}
c# reflection autofac
marked as duplicate by Robert Harvey♦
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.
add a comment |
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
}
c# reflection autofac
marked as duplicate by Robert Harvey♦
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 wordenum
into the title might be of use? egHow 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
add a comment |
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
}
c# reflection autofac
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
c# reflection autofac
edited Nov 23 '18 at 4:18
pixelbits
asked Nov 23 '18 at 2:36
pixelbitspixelbits
40k1065102
40k1065102
marked as duplicate by Robert Harvey♦
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♦
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 wordenum
into the title might be of use? egHow 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
add a comment |
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 wordenum
into the title might be of use? egHow 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
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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? egHow 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