So I am wanting to refactor this to LinQ, and not having any luck with it and I am out of search options to guide me in the right direction.
Can someone explain this to me please, as the commented code works and the LinQ well I know I am doing it wrong. Just can't find any information to guide me along.
public static T Get<T>()
{
Type type = typeof(T);
if (IsRegistered(type))
{
//foreach(var service in _services)
//{
// var test = service.InstanceClass;
// if(test.GetType().Equals(type))
// {
// return (T)test;
// }
//}
return (T) _services.Select(x => x.InstanceClass.GetType() == type);
}
return default;
}
CodePudding user response:
First of all, you want to return T
, not bool
that's why the final Select
is incorrect:
// Returns `bool`: if "x.InstanceClass" is of type "type"
Select(x => x.InstanceClass.GetType() == type)
Now let's state the problem:
- From
_services
we want to return firstservice.InstanceClass
which is oftype
type - If there are no such items, return default
We can do it like this:
return _services
.Select(item => item.InstanceClass)
.OfType<T>()
.FirstOrDefault(item => item.GetType() == type);
A brief explanation on what's going on:
- We should return some
item.InstanceClass
from_services
so putSelect
. - We are interesting in
InstanceClass
which can be treated as in instance of typeT
- that's a purpose ofOfType<T>()
. - Finally, we want
InstanceClass
to be exaclty of typeT
(and not say, of a derived type) and we putFirstOrDefault
with condition.
The method can look like this:
public static T Get<T>() {
// Let's check first for special and edge cases
if (!IsRegistered(typeof(T)))
return default;
return _services
.Select(item => item.InstanceClass)
.OfType<T>()
.FirstOrDefault(item => item.GetType() == typeof(T));
}
CodePudding user response:
Select
returns an IEnumerable<bool>
and you're casting it to T
, so make sure you using Where
and do a FirstOrDefault()
to get only out value.
Try this:
return (T)_services.Where(x => x.InstanceClass.GetType() == type).FirstOrDefault();