[Ironpython-users] Embedded IronPython, Properties with Custom Attributes, __clrtype__ and PropertyGrid
Borut Jarc
spartitraffico at gmail.com
Tue Sep 13 11:46:12 EDT 2016
Hello,
I'm Borut Jarc and I work at A.S.U.I.Ts (Azienda Sanitaria Universitaria
Integrata di Trieste) as a Domain Admin.
I'm writing an C# 4.0 application to help my Network Admins to manage the
subnets configured on our network switches.
The application has Iron Python embedded, a simple plugin system and a
plugin manager. The plugins are written in IronPython.
Everything works fine so now I am writing a configuration dialog for the
plugins properties.
I'm trying to use a PropertyGrid which I give an instance of an IronPython
class.
To make use of all the nice features of PropertyGrid like Description,
Category and PasswordPropertyText i extended the ClrClass from clrtype
module from clrtye sample, implemeting the emit_property method as follows:
class MyClrClass(ClrClass):
def emit_property(self, typebld, prop, name, clrtype):
prpbld = typebld.DefineProperty(name, PropertyAttributes.None,
clrtype, None)
if prop.fget:
getter = self.emitted_methods[(prop.fget.func_name,
prop.fget.arg_types)]
prpbld.SetGetMethod(getter)
if prop.fset:
setter = self.emitted_methods[(prop.fset.func_name,
prop.fset.arg_types)]
prpbld.SetSetMethod(setter)
if hasattr(prop.fget, "CustomAttributeBuilders"):
for cab in prop.fget.CustomAttributeBuilders:
prpbld.SetCustomAttribute(cab)
then in my IronPython script I define the class:
class OSCommware7_ssh(PluginBase):
__metaclass__ = clrtype.MyClrClass
_clrnamespace = "PianoIndirizzamentiIP_Tool"
_clrfields = {"_username":str,
"_password":str,
"_prompt":str}
Description = clrtype.attribute(DescriptionAttribute)
Category = clrtype.attribute(CategoryAttribute)
@property
@Category("Credentials")
@Description("Username for login on Core Switch")
@clrtype.accepts()
@clrtype.returns(str)
def username(self): return self._username
@username.setter
@clrtype.accepts(str)
@clrtype.returns()
def username(self, value): self._username = value
....
(PluginBase is the c# class with acts as the plugin skeleton. Contais also
some plugin helper functions (password encryption decryption))
and in my c# code I do something like:
...
_engine = Python.CreateEngine();
_scope = _engine.CreateScope();
_runtime = _engine.Runtime;
_root_dir = AddAssemblies();
_scope = _engine.CreateScope();
...
ScriptSource script =
_engine.CreateScriptSourceFromFile(path_to_python_script);
CompiledCode code = script.Compile();
script.Execute(scope);
...
dynamic plugin_instance = my_engine.scope.GetVariable("OSCommware7_ssh")()
propertyGrid1.SelectedObject = plugin_instance;
...
If I inspect plugin_instance with
(System.Reflection.PropertyInfo[])(plugin_instance.GetType().GetProperties()):
-
((System.Reflection.PropertyInfo[])(plugin_instance.GetType().GetProperties()))[3]
{System.String
username} System.Reflection.PropertyInfo
{System.Reflection.RuntimePropertyInfo}
Attributes None System.Reflection.PropertyAttributes
CanRead true bool
CanWrite true bool
- CustomAttributes Count = 2
System.Collections.Generic.IEnumerable<System.Reflection.CustomAttributeData>
{System.Collections.ObjectModel.ReadOnlyCollection<System.Reflection.CustomAttributeData>}
+ [0] {[System.ComponentModel.DescriptionAttribute("Username for login on
Core Switch")]} System.Reflection.CustomAttributeData
+ [1] {[System.ComponentModel.CategoryAttribute("Credentials")]}
System.Reflection.CustomAttributeData
+ Raw View
+ DeclaringType {Name = "OSCommware7_ssh" FullName =
"PianoIndirizzamentiIP_Tool.OSCommware7_ssh"} System.Type
{System.RuntimeType}
+ GetMethod {System.String username()} System.Reflection.MethodInfo
{System.Reflection.RuntimeMethodInfo}
IsSpecialName false bool
MemberType Property System.Reflection.MemberTypes
MetadataToken 385875970 int
+ Module {Snippets.scripting} System.Reflection.Module
{System.Reflection.Emit.InternalModuleBuilder}
Name "username" string
+ PropertyType {Name = "String" FullName = "System.String"} System.Type
{System.RuntimeType}
+ ReflectedType {Name = "OSCommware7_ssh" FullName =
"PianoIndirizzamentiIP_Tool.OSCommware7_ssh"} System.Type
{System.RuntimeType}
+ SetMethod {Void username(System.String)} System.Reflection.MethodInfo
{System.Reflection.RuntimeMethodInfo}
+ Non-Public members
I see all the properties and their CustomAttributes set as expected, but
for some reason PropertyGrid simply ignore them.
I feel like I'm missing something important but I don't know what.
Can someone explain me what I'm doing wrong?
Thank you and forgive my poor English...
Borut Jarc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20160913/828aea9c/attachment.html>
More information about the Ironpython-users
mailing list