Passing arguments to C# generic new() of templated type
In order to create an instance of a generic type in a function you must constrain it with the "new" flag.
public static string GetAllItems<T>(...) where T : new()
However that will only work when you want to call the constructor which has no parameters. Not the case here. Instead you'll have to provide another parameter which allows for the creation of object based on parameters. The easiest is a function.
public static string GetAllItems<T>(..., Func<ListItem,T> del) {
...
List<T> tabListItems = new List<T>();
foreach (ListItem listItem in listCollection)
{
tabListItems.Add(del(listItem));
}
...
}
You can then call it like so
GetAllItems<Foo>(..., l => new Foo(l));
C# generics problem - newing up the generic type with parameters in the constructor
Jared's answer is still a good way to go - you just need to make the constructor take the Func<PageData, T>
and stash it for later:
public class HomepageCarousel<T> : List<T> where T: IHomepageCarouselItem
{
private readonly Func<PageData, T> factory;
public HomepageCarousel(Func<PageData, T> factory)
{
this.factory = factory;
}
private List<T> GetInitialCarouselData()
{
List<T> carouselItems = new List<T>();
if (jewellerHomepages != null)
{
foreach (PageData pageData in jewellerHomepages)
{
T homepageMgmtCarouselItem = factory(pageData);
carouselItems.Add(homepageMgmtCarouselItem);
}
}
return carouselItems;
}
Then you just pass the function into the constructor where you create the new instance of the HomepageCarousel<T>
.
(I'd recommend composition instead of inheritance, btw... deriving from List<T>
is almost always the wrong way to go.)
Is is possible to make a generic method with two type arguments only infer one of them?
Yes, you always need to specify both types. Type inference only works when you specify all the type arguments.
Yes, the C# compiler cannot infer types based on constraints. It can only infer the types based on the types you pass in as arguments.
On a side note, there is an open issue about improving type inference in this regard, however it doesn't seem to have high priority.
Passing generic type to non generic methods
It is not possible for now.
you should costrain the generic type to something the compiler can narrow down to one of the overloads of the BitConverter.GetBytes(value). The struct you used to constrain is a very larger group than just one of the overloads, so it won't let you proceed.
With c#10 you might have one chance with the feature 'extend everything', but it's not yet released won't have much sense to talk about that now.
Is it possible to access the type generic T without parameters (C#)?
It sounds like you should use
var y = typeof(T).Name; // y is "SomeClass"
or, if you want the namespace too,
var y = typeof(T).FullName; // y is "Namespace.For.SomeClass"
instead.
nameof
will, as you discovered, return the name of the type parameter (i.e. "T"
) and not the name of the type that the generic method is invoked with (i.e. "SomeClass"
).
Related Topics
Deserializing Json Data to C# Using Json.Net
If Async-Await Doesn't Create Any Additional Threads, Then How Does It Make Applications Responsive
How to Increase the Max Upload File Size in Asp.Net
Difference Between Shadowing and Overriding in C#
Find Control by Name from Windows Forms Controls
How to Get the Cpu Usage in C#
Is There Any Significant Difference Between Using If/Else and Switch-Case in C#
How to Handle Accessviolationexception
A Generic Error Occurred in Gdi+, Jpeg Image to Memorystream
How to Specify a Custom Location to "Search For Views" in ASP.NET MVC
How to Split CSV Whose Columns May Contain ,
C# Data Connections Best Practice
How to Check For Internet Connectivity Using .Net
Why Use the 'Ref' Keyword When Passing an Object
How to Migrate a .Net Windows Service Application to Linux Using Mono