Create list of variable type
You could use Reflections, here is a sample:
Type mytype = typeof (int);
Type listGenericType = typeof (List<>);
Type list = listGenericType.MakeGenericType(mytype);
ConstructorInfo ci = list.GetConstructor(new Type[] {});
List<int> listInt = (List<int>)ci.Invoke(new object[] {});
Create List by type variable
If your just creating a new List
your not needing it. But if you want to add that element to the list or just have the empty list by Type for some reason here's some examples:
public static <T> List<T> createMutableListByType(final Class<T> tClass){
return new ArrayList<T>();
}
public static <T> List<T> createImmutableListWithType(final T type){
return List.of(type);
}
public static <T> List<T> createMutableListWithType(final T type){
final List<T> result = new ArrayList<>();
result.add(type);
return result;
}
create List of Type using Type variable at runtime
It's not really clear how you expect this could possibly work, especially given the comment:
// Provides a List, I don't have to place LinkAddy
// but its a List<object> and not List<LinkAddy> so wherever I use
// the objects, I'll have to cast the object like
// string title = (fromQuery_IsDiff[0] as LinkAddy).Title;
If you know enough to cast to LinkAddy
(which would be better done as a cast than using as
, IMO) then you clearly know the type at compile time. If you know the type at compile-time, just supply it as a type argument.
Things like MakeGenericType
are useful when you don't know the type involved at compile-time, which means you can't cast... and you can't use any of the other things you know about the type anyway.
You could potentially use dynamic typing instead... but if you know that there will be a Title
property, perhaps what you really want is an interface instead, expressing the commonality. Then you could potentially use generic methods which have a constraint on that interface.
Anyway, you should think about which things you know at compile-time and which things are only known at execution time. For the most part, generics is aimed at types known at compile-time... while using generics at execution time via reflection is possible, it's sort of fighting against the design, so you shouldn't be surprised when it's frustrating.
Creating a list of variable types in R
Call class on your data frame using lapply:
lapply(data_frame, class)
Create List from runtime type
You can use reflection:
List<object> database = new List<object>();
foreach (Type t in Types)
{
var listType = typeof(List<>).MakeGenericType(t);
database.Add(Activator.CreateInstance(listType));
}
Variable type Liststring In object, how can I make a List of this object
list2
is a list:
List<ValidDay> list2 = new List<ValidDay>();
But you're trying to set an object to it:
list2 = new ValidDay() { /.../ };
Don't set it to an object, simply add the object to it:
list2.Add(new ValidDay() { /.../ });
Python list type declaration
TypeError: 'type' object is not subscriptable
Python 3.9 allows for list[str]
. Earlier you had to import List
from typing
and do -> List[str]
.
NameError: name 'Point' is not defined
If you want to declare the type of "self" you can either put that in a string def isSamePoint(self, p: "Point") -> bool:
or create an interface.
>>> class A: pass
>>> class B(A): pass
>>> b = B()
>>> isinstance(b, A)
True
so def isSamePoint(self, p: A)
would do the trick.
Also, if you want to check if isSamePoint
you might want to consider your own __eq__
.
Related Topics
Getting All Possible Combinations from a List of Numbers
C# Winforms: How to Set Main Function Stathreadattribute
Windows Forms Graphic Issue on Windows 10 Os
ASP.NET MVC Route to Catch All *.Aspx Requests
How to Draw Shapes in Winforms
.Net Core Identity Server 4 Authentication VS Identity Authentication
Convert Array of Integers to Comma-Separated String
Is Async/Await Suitable for Methods That Are Both Io and CPU Bound
Workaround for the Waithandle.Waitall 64 Handle Limit
A Textbox/Richtextbox That Has Syntax Highlighting? [C#]
How to Embed Multiple Images in Email Body Using .Net
Dynamically Added Dropdownlists Are Not Firing Selectedindexchanged Event
Trying to Debug Windows Store App from Dump Files
Refactoring Code to Avoid Anti-Pattern
Http Post Returns Error: 417 "Expectation Failed."