c#


Check if FieldInfo type is generic class without type


I've got a generic class (ClassName<T>) and I've got several FieldInfos for fields with different Ts (Class<bool>, Class<int>, etc...). How do I check if the FieldInfo's type just is ClassName, not ClassName<bool> or ClassName<int>? Here's my code:
public class Field<T>
{
public T value;
public Field(T value)
{
this.value = value;
}
}
...
public class Action_SetPosition : Action
{
public Field<GameObject> gameObject = new Field<GameObject>(null);
public Field<Vector3> position = new Field<Vector3>(Vector3.zero);
}
...
FieldInfo[] fields = typeof(Action_SetPosition).GetFields();
for (int a = 0; a < fields.Length; a++)
{
if (fields[a].FieldType == typeof(Field<ANYTHING>))
{
}
}
I want to check it the type of fields[a] has Field in it, and ANY <T>. Thanks. :)
I want to check it the type of fields[a] has Field in it, and ANY . Thanks. :)
I'm not quite sure I understand this statement but I think you are looking for something like this:
if(fields[a].IsGenericType &&
fields[a].FieldType.GetGenericTypeDefinition() == typeof(Field<>))
This will return true if the field type is Field<T> regardless of the type of T...
One way would be with reflection.
But it's kind of ugly and imperformant if you do not cache.
An easier way is with an interface
You could try this
public interface IField {}
public class Field<T> : IField
{
public T value;
public Field(T value)
{
this.value = value;
}
}
// ...
FieldInfo[] fields = typeof(Action_SetPosition).GetFields();
for (int a = 0; a < fields.Length; a++)
{
if (fields[a] is IField)
{
// ...
}
}

Related Links

Try in finally block
is there any way to use generic class/method with 2 type that will not be the same? [duplicate]
Create array C# (.NET)
How to custom draw a cursor handle on MS Chart
Get Microphone Input Level Naudio
Building an orthographic camera for Unity3D, how can I keep four specific targets in sight?
Resolving Constructor dependencies from another objects properties with Unity
How can I reuse anonymous properties already computed
Ektron: Getting content by folder ID - get all smartforms in folder?
Static constructor in Singleton design pattern
How to set data-attribues with ASP.NET?
making a .lnk with powershell not working
Checking if List<Tuple<T, T>> is empty
Rename HTML name attribute to get data into model
C# Create constructor for a class from outside of the project
Given a collection of Locations, how can I determine the appropriate Zoom level and Map Center for Bing Maps?

Categories

HOME
logging
google-cloud-pubsub
ftp
packet
salesforce-lightning
lithium
coordinates
opengl-es
sdk
atlassian-fisheye
aggregate-functions
android-webview
revit-api
apache-httpclient-4.x
jcodemodel
element
javascript-debugger
yui
template10
hapijs
indri
beacon
django-rest-auth
passbook
summernote
jq
struts
pydub
azure-table-storage
federated
edirectory
schedule
exacttarget
imdb
webclient
n1ql
mootools
ponylang
abide
redhat-brms
cowboy
sknode
wicket-tester
glide-image-library
flume-ng
inner-join
xmldom
easyquery
skype-bots
wink
ajv
nservicebus6
dynatable
disassembly
stripes
gevent
joystick
tinybox2
geokit
vga
jigsaw
recurring
change-password
sprockets
hotkeys
jde
jcalendar
code-translation
methodology
apache2-module
bluetooth-lowenergy-4.2
google-app-engine-php
cascading
php-gd
self-join
openhab
nmock
tokudb
stderr
data-quality-services
os.walk
cocos2d-x-2.x
oxygenxml
opencobol
pidgin
vbaccelerator
randomaccessfile
pechkin
caroufredsel
source-depot
nx
generator-expression
configurationsection
inflate
aspmenu-control
beep
systrace
dancer
xpolog
opendir
funambol
hwnd
arraycollection
datarelation
activescaffold
newspeak
hp-trim
ihtmldocument2
jettison

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile