is it allowed to use reflection to implement a x# runtime function ? I ask because i currently try to implement the VFP GetFont() function. Unfortunately, the Setoption() method of the .net FontDialog class is an internal method, but fortunately this method is accessible via reflection. I´m using this method to disable the script combobox and to enable the font description.
Here´s a snippet how my code currently looks like:
oDlg := FontDialog{}
oMi := TypeOf(FontDialog):GetMethod("SetOption", BindingFlags.NonPublic | BindingFlags.Instance)
IF oMi != NULL
// this enables the font description that is always shown in the left/bottom area of the VFP GetFont() dialog
oMI:Invoke(oDlg, { CF_PRINTERFONTS, TRUE })
// btw. Its possible to include fonts like Courier, FixedSys, Terminal
// But since the .net FontDialog supports true type fonts only, trying to select e.g. FixedSys
// would trigger the .net runtime error "only true type fonts are supported"!
//
// oMI:Invoke(oDlg, { 0x00040000L , FALSE }) // CF_TTONLY
ENDIF
oDlg:ShowEffects := FALSE
oDlg:ShowHelp := TRUE
oDlg:MinSize := 4
oDlg:MaxSize := 127
lReturnCharSet := IIF ( IsNil ( nFontCharSet ) , FALSE , TRUE )
IF oMi != NULL .and. ! lReturnCharSet
// this disables the script combobox
oMI:Invoke(oDlg, { CF_NOSCRIPTSEL, TRUE })
ENDIF
Using reflection is perfectly fine, as long as you add a check to make sure that the method indeed exists (like you are doing in your code above)
In this case (of the FontDialog) it is very unfortunate that they have made the method internal. I see no reason to do that at all.
In theory you could subclass FontDialog and try to do it from there, but the options field is private (not protected) and the SetOptions is internal.
Robert
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Problem is though that AFAIK, admins can configure security settings in such way that such reflection "cheats" will cause runtime exceptions.
So if possible I think it's better to avoid this. But if there's absolutely no other way...
I think for now it would be useful if the Foxpro guys take a look at the current GetFont() implementation (see the attached viaef) and report if GetFont() works as expected.
regards
Karl-Heinz
Attachments
[The extension viaef has been deactivated and can no longer be displayed.]