Linked Questions
16 questions linked to/from Setting generic type at runtime
0 votes
1 answer
801 views
How to convert string value to generic type parameter? [duplicate]
How can I pass a generic method type parameter as a variable to it? Below given is the crud example of what I want to achieve. It's just for a demo purpose, not the actual code. I can use if...else or ...
1 vote
1 answer
511 views
Register a type at runtime using Unity Container [duplicate]
Why can I not do this? Where obj value is a valid type. Type type = obj.Value.GetType(); this.unityContainer.RegisterType<type>(); OR this.unityContainer.RegisterType(obj.Value); When I can do ...
-1 votes
1 answer
179 views
How can I use string value for passing Type for Type<T> parameter? [duplicate]
I am trying to create a list of object of Student in School. Instead of Student the School can also have Teacher as a list. I want to use the class name as a parameter instead of the actual class type ...
89 votes
2 answers
47k views
Creating a Generic<T> type instance with a variable containing the Type
Is it possible to achieve the following code? I know it doesn't work, but I'm wondering if there is a workaround? Type k = typeof(double); List<k> lst = new List<k>();
5 votes
4 answers
6k views
Reflection: Calling Method with generic list as a result
I have the following example-class: public class MyClass<T> { public IList<T> GetAll() { return null; // of course, something more meaningfull happens here... } } And ...
3 votes
4 answers
8k views
How to cast an object value to Type values?
I have a type like this: public class TypeValue { public Type Type { get; private set; } public object Value { get; private set; } } so I can do something like: TypeValue tv = ... int count =...
1 vote
3 answers
1k views
use Type.GetType(string) in C# 2.0 .net but type or namespace name 't' could not be found
I'm not sure how to correct this. I have public void get_json(String TYPE) { Type t = Type.GetType("campusMap." + TYPE); t[] all_tag = ActiveRecordBase<t>.FindAll(); } But I always ...
1 vote
1 answer
1k views
Passing variable type to generic method
I have a generic method : public static T GetSetting<T>(string Key){ .... } Which returns a setting value from database. Now I'm trying to make it fill the class automatically with Settings: ...
1 vote
1 answer
1k views
Is there a way to set a generic type to a Blazor component at runtime?
<GenericInputFor> provides markup based on the argument it receives and its type, it behaves fine with compile time types, but when I try to iterate through some class properties with reflection ...
0 votes
0 answers
455 views
How can I set a generic global state and accessing it in a blazor component
I am working on a razor component library in which I would like to expose some components. However I would like to have the user of the library be able to set some global (generic) object that all ...
-1 votes
2 answers
177 views
C# express a type variable as a type [duplicate]
Let's say I'm working with generic function : public interface IFoo { Type Type; TValue Read<TValue>(); } public class Foo : IFoo {} var f = new Foo(); I'd like to write : var value = ...
0 votes
2 answers
205 views
Why Compiler is not allowing to pass Class.System.Type as T argument to a generic method [duplicate]
Compilation errors: Visual Basic.NET Compilation error: "Type t is not defined" C# Compilation error: 't' is a variable but is used like a type Why i can not do this: VB: For i = 0 To dt....
1 vote
2 answers
101 views
Find out if class implements an interface thru reflection
I have the following class in c#: public class WorkOrderStatus : ICustomEnum<WorkOrderStatus> { } during runtime I need to figure out if a property of the following class implements the custom ...
0 votes
2 answers
151 views
How to convert a string into a class reference and send that to a generic class
I have realized that there are questions that sounds similar to mine but they do not solve my problem. So I want to change a string into a class reference but I cannot change into a variable of type ...
1 vote
1 answer
61 views
Call extension method for type from instance extension
I've extended the Enum type as such: public class Enum<TEnum> where TEnum : notnull, Enum, IConvertible { public static int Length => Enum.GetNames(typeof(TEnum)).Length; } I've also ...