I’m trying to use code such as the following Objective-C in MonoTouch:
if (NSClassFromString(@"ADBannerView"))
What is the equivalent in C#?
Basically I’m wanting to use iAd, but I need to check for the existance of ADBannerView and ADInterstitialAd, because they are not available on all versions of the OS. (And I’d rather do feature checking than iOS version checking)
I think this could be helpful in other situations as well.
A recent MonoTouch will always provide
ADBannerViewso you cannot the the C# equivalent ofType.GetTypeto query availability.Normally a version check is the best way to check for features. E.g.
will return true for any 4.0+ versions of iOS (4.0 being when
ADBannerViewwas added to iOS).A possible alternative (might not work in every case) is to create an instance and check it’s handle. Since ObjC is message based sending
initwill return null (something a .NET constructor can’t do). E.g.Note that you probably best surround the above with
usingto dispose the view or integrate this inside the normal creation of yourADBannerView.UPDATE: of course a p/invoke to
NSClassFromStringwould do exactly the same as the ObjectiveC code 🙂