Reputation: 1546
I am trying to see if it is possible to use a System.Type in a variable in a comparison.I have the following code:
internal ObservableCollection<FREQUENCY> GetFrequencies(System.Type equipmenttype)
{
...
foreach (var incident in query)
{
if (typeof(equipmenttype).IsSubclassOf(typeof(incident)))
{
foreach (var freq in incident.FREQUENCY)
{
freqs.Add(freq);
}
}
}
return freqs;
}
But the variables 'tmp' and 'equipmenttype' pull the error "The type or namespace name 'tmp' could not be found (are you missing a using directive or an assembly reference?)"
I understand that usually this is used by saying typeof(MYCLASS), but I was curious if this was possible using a variable of System.Type, or if there was any way to do that. Thanks.
Upvotes: 0
Views: 67
Reputation: 161773
Try if (equipmenttype.IsSubclassOf(incident.GetType())
. equipmenttype
is already a System.Type
, and GetType()
must be called to determine the type of an instance.
Upvotes: 2
Reputation: 73462
I can't see where is tmp
in you code. but sure you're missing this
if (typeof(equipmenttype).IsSubclassOf(typeof(incident)))
Should be
if (equipmenttype.IsSubclassOf(incident.GetType()))
typeof
operator is used to get the RuntimeType
of a Type. But you already got RuntimeType
here which is equipmenttype
, So you don't need to use typeof
here.
Upvotes: 4