|
Not that I know of - although you can add constraints to a generic method, there is no way to say "where T : !IEnumerable" unfortunatly.
You could check the type in a single method and route it to one of two private methods?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: You could check the type in a single method and route it to one of two private methods?
I tried that, and it didn't want to work.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Strange - it works for me:
public void DoSomething()
{
MyClass m = new MyClass();
List<MyClass> list = new List<MyClass>() { m };
Console.WriteLine($"{ProcessDepending(list)}, {ProcessDepending(m)}");
}
public int ProcessDepending<T>(T instance)
{
if (instance is System.Collections.IEnumerable collection)
{
return ProcessCollection(collection);
}
return ProcessInstance(instance);
}
private int ProcessInstance<T>(T instance)
{
return 1;
}
private int ProcessCollection(IEnumerable collection)
{
return 2;
}
private class MyClass { }
I get 2, 1 as I expected.
The only thing I can't figure out is a way to get the collection in a form that I can define this:
private int ProcessCollection<T>(IEnumerable<T> collection) Because I can't get at the type of the collection objects at compile time ...
I can do this though:
public int ProcessDepending<T>(T instance)
{
if (instance is IEnumerable<object> collection)
{
return ProcessCollection<object>(collection);
}
return ProcessInstance(instance);
}
private int ProcessCollection<T>(IEnumerable<T> collection)
{
return 2;
}
And it'll work. Messy ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Yeah, I came with all that too, but since I had to create a new method anyway, I decided not to do that.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
this works using one overload in .NET 4.8; don't know about earlier releases:
private IEnumerable<T> DoSomething<T>(
Func<T,T> func,
params T[] args)
{
foreach (T t in args)
{
yield return func(t);
}
}
private IEnumerable<T> DoSomething<T>(
Func<T,T> func,
IEnumerable<T> args)
{
return DoSomething(func, args.ToArray());
}
I don't claim it's elegant
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Point of order - I'm not returning a collection of generics, so it can't pick the appropriate overload.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
#realJSOP wrote: I'm not returning a collection of generics, so it can't pick the appropriate overload. Look again: this solution works: it's on you to adapt it to your specific needs.
Of course, I am assuming your choice of using the same method name (overloading) is rational, and that it implies one instance of T is handled the same way as multiple instances.
See my reply to Deeming, here.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
If you call MyMethod with something that has a compile-time type of IEnumerable<T> , you'll call the correct method.
If you call it with something where the compile-time type implements IEnumerable<T> , you'll call the wrong method.
MyMethod(Enumerable.Range(1, 42));
MyMethod(new[] { 1, 2, 3 });
MyMethod(new List<int> { 1, 2, 3 });
One workaround might be to add .AsEnumerable() to the method argument.
MyMethod(Enumerable.Range(1, 42));
MyMethod((new[] { 1, 2, 3 }).AsEnumerable());
MyMethod((new List<int> { 1, 2, 3 }).AsEnumerable());
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Richard, using these tests with the code I posted here, I believe 'AsEnumerable may not be necessary:
var t0 = DoSomething(
t => t * 3,
100).ToList();
var t1 = DoSomething(
t => t * 3,
1,2,3,4,5).ToList();
var t2 = DoSomething(
t => t * 3, Enumerable.Range(6, 10)).ToList();
var t3 = DoSomething(
t => t * 3, new[] { 11, 12, 13 }).ToList();
var t4 = DoSomething(
t => t * 3, new List<int> { 14, 15, 16 }).ToList(); Of course, I am assuming J's choice of using the same method name (overloading) is rational, and that it implies one instance of T is handled the same way as multiple instances.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
If you start with the methods from John's post, you'll see the issue:
public static void Foo<T>(T value) => Console.WriteLine("Foo a single value");
public static void Foo<T>(IEnumerable<T> values) => Console.WriteLine("Foo multiple values");
public static void Bar()
{
Foo(42);
Foo(Enumerable.Range(1, 42));
Foo(new[] { 1, 2, 3 });
Foo(new List<int> { 1, 2, 3 });
Foo((new[] { 1, 2, 3 }).AsEnumerable());
Foo((new List<int> { 1, 2, 3 }).AsEnumerable());
} Generic Overload Resolution | C# Online Compiler | .NET Fiddle[^]
The question was, given an x whose compile-time type implements IEnumerable<> , is there a way to make Foo(x) call Foo(IEnumerable<T>) instead of Foo(T) , without modifying the call-site.
The answer seems to be that there isn't. If the compile-time type of x is not exactly equal to IEnumerable<> , then the compiler prefers to call the single-value method, using the compile-time type as the type parameter, rather than casting to IEnumerable<T> and calling the multi-value method.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Richard, Maybe the 103F+ days, and air pollution over-the-toxic-top, and "sheltering in place," and ... gosh: old age ? ... are creeping up on me , so I am denser than usual, but ...
doesn't the code I posted (and the tests) demonstrate a working solution with only one overload to the issue here ?
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
<crickets>
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
Wed. ... GMT +7 ... 105F at 4PM
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Yikes!
10°C / 50°F here at 1PM. Last night went down to -1°C / 30°F.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Not as far as I can see.
public static void Foo<T>(IEnumerable<T> value) => Console.WriteLine($"Foo<{typeof(T)}>(IEnumerable<{typeof(T)}>)";
public static void Foo<T>(params T[] value) => Console.WriteLine($"Foo<{typeof(T)}>(params {typeof(T)}[])";
public static void Bar()
{
Foo(42);
Foo(Enumerable.Range(1, 42));
Foo(new[] { 1, 2, 3 });
Foo(new List<int> { 1, 2, 3 });
} Generic Overload Resolution 2 | C# Online Compiler | .NET Fiddle[^]
If you pass a List<> to the method, it calls the params overload passing the entire list as the first parameter. The type parameter will be List<> , not the type of the list's elements.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yep, all I had to do was:
MyMethod(data.AsEnumerable());
The data object is indeed a List<T> , and I thought that .Net would know how to handle it without me taking steps.
Many thanks, oh Great and Powerful Oz.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
OK, the "simple" method is to cast it:
MyMethod((IEnumerable<int>)data);
which is, of course, really ugly. So, hide it in another overload:
public void MyMethod<T>(List<T> data)
{
MyMethod<T>((IEnumerable<T>)data);
}
Truth,
James
|
|
|
|
|
Whenever I can write the excel file from stored procedure but one field size is 12 digit.so It can return exponential number.How to return these actual number?
|
|
|
|
|
|
I think that you will find it is actually returning the number. Select the cell in the spreadsheet and look at the value in the formula bar. That should have the digits in it. The issue is that the default display for a large number is as an exponential format but the display is not what is actually stored.
Try, as a counter test, writing a (say) 15 digit number in a cell and then move to another cell then look at what the cell you wrote into has as its displayed value.
Update:
You can 'see' longer numbers in non-exponential format. Right click the cell(s), select Format Cells, Number tab / Category Number, set Decimal places to 0.
modified 2-Apr-20 9:40am.
|
|
|
|
|
"C# Oracle SYSDBA connection string not able to working on windows 10? But the same connection string is working on windows 7!"
|
|
|
|
|
It's probably not the connection string (though we have no idea what string you are using, how you are using it, or where your code is running).
More likely, it's a firewall / port blocking / security issue.
Talk to your Oracle DBA and see what he says first.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Not sure what you want us to do. We can't see anything that you have done.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
I am using the below code to obtain a list of all local user accounts on the machine. But in addition to the "human" accounts, it returns all the other accounts, such as the SQLSERVER accounts and something called the WDAGUtilityAccount .
What is the property I should be checking for to determine which listed accounts are the "human" accounts, and which are machine accounts?
public static List<string> GetComputerUsers()
{
List<string> users = new List<string>();
var path =
string.Format("WinNT://{0},computer", Environment.MachineName);
using (var computerEntry = new DirectoryEntry(path))
{
foreach (DirectoryEntry childEntry in computerEntry.Children)
{
if (childEntry.SchemaClassName == "User")
{
users.Add(childEntry.Name);
}
}
}
return users;
}
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|