I have an array of Type objects corresponding to int, bool, string, float, int? …
How do I write a function that takes in the above array and returns strongly typed default values for each type in the array?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To get a default value from a
Type, simply invokeActivator.CreateInstanceAs for the bit about getting a strong type, the problem is that when you use reflection in this manner, it deals in objects. To get a strong type, you would need to know it at compile time, which kind of defeats your purpose. When you’re doing these things at runtime, you’re left with
objectordynamic(which is justobjectbehind the scenes).Beyond that, if you’re talking about running the array through a method and returning default values for each type, you’re going to be talking about returning an
IEnumerable<object>orobject[]array, as the type of each item would obviously differ.