PageRenderTime 749ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_2_6/Src/IronPythonTest/TypeDescriptor.cs

#
C# | 166 lines | 108 code | 29 blank | 29 comment | 10 complexity | 084257c494bf5882c9c9744dcda4d333 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if !SILVERLIGHT // TypeDescriptor, COM
  16. using System;
  17. using System.Collections;
  18. using System.ComponentModel;
  19. using System.Runtime.InteropServices;
  20. namespace IronPythonTest {
  21. /// <summary>
  22. /// Test cases for verifying IronPython plays nicely with reflection and you
  23. /// can reflect over python objects in the proper way.
  24. /// </summary>
  25. public class TypeDescTests {
  26. public object TestProperties(object totest, IList shouldContain, IList shouldntContain) {
  27. if (shouldContain != null) {
  28. foreach (object o in shouldContain) {
  29. bool fFound = false;
  30. foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(totest)) {
  31. if ((string)o == pd.Name) {
  32. fFound = true; break;
  33. }
  34. }
  35. if (!fFound) return false;
  36. }
  37. }
  38. if (shouldntContain != null) {
  39. foreach (object o in shouldntContain) {
  40. bool fFound = false;
  41. foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(totest)) {
  42. if ((string)o == pd.Name) {
  43. fFound = true;
  44. break;
  45. }
  46. }
  47. if (fFound) return false;
  48. }
  49. }/*
  50. foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(totest)) {
  51. if (shouldContain != null && !shouldContain.Contains(pd.Name)) {
  52. Console.WriteLine("No {0}", pd.Name);
  53. return Ops.FALSE;
  54. } else if (shouldntContain != null && shouldntContain.Contains(pd.Name)) {
  55. return Ops.FALSE;
  56. }
  57. }*/
  58. return true;
  59. }
  60. public object GetClassName(object totest) {
  61. return TypeDescriptor.GetClassName(totest);
  62. }
  63. public object GetComponentName(object totest) {
  64. return TypeDescriptor.GetComponentName(totest);
  65. }
  66. public object GetConverter(object totest) {
  67. return TypeDescriptor.GetConverter(totest);
  68. }
  69. public object GetDefaultEvent(object totest) {
  70. return TypeDescriptor.GetDefaultEvent(totest);
  71. }
  72. public object GetDefaultProperty(object totest) {
  73. return TypeDescriptor.GetDefaultProperty(totest);
  74. }
  75. public object GetEditor(object totest, Type editorBase) {
  76. return TypeDescriptor.GetEditor(totest, editorBase);
  77. }
  78. public object GetEvents(object totest) {
  79. return TypeDescriptor.GetEvents(totest);
  80. }
  81. public object GetEvents(object totest, Attribute[] attributes) {
  82. return TypeDescriptor.GetEvents(totest, attributes);
  83. }
  84. public object GetProperties(object totest) {
  85. return TypeDescriptor.GetProperties(totest);
  86. }
  87. public object GetProperties(object totest, Attribute[] attributes) {
  88. return TypeDescriptor.GetProperties(totest, attributes);
  89. }
  90. public object CallCanConvertToForInt(object totest) {
  91. return TypeDescriptor.GetConverter(totest).CanConvertTo(typeof(int));
  92. }
  93. }
  94. /// <summary>
  95. /// Registration-free COM activation
  96. /// </summary>
  97. [ComImport]
  98. [Guid("00000001-0000-0000-C000-000000000046")]
  99. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  100. public interface IClassFactory {
  101. [return: MarshalAs(UnmanagedType.Interface)]
  102. object CreateInstance(
  103. [MarshalAs(UnmanagedType.IUnknown)]object pOuterUnk,
  104. ref Guid iid);
  105. void LockServer(bool Lock);
  106. }
  107. public class ScriptPW {
  108. [DllImport("scriptpw.dll", PreserveSig = false)]
  109. static extern void DllGetClassObject(
  110. [In] ref Guid rclsid,
  111. [In] ref Guid riid,
  112. [MarshalAs(UnmanagedType.Interface)][Out] out object ppv);
  113. [DllImport("ole32.dll", PreserveSig = false)]
  114. public static extern void CoRegisterClassObject(
  115. [In] ref Guid rclsid,
  116. [MarshalAs(UnmanagedType.IUnknown)] object pUnk,
  117. /* CLSCTX */ int dwClsContext,
  118. /* REGCLS */ int flags,
  119. out uint lpdwRegister
  120. );
  121. [DllImport("ole32.dll", PreserveSig = false)]
  122. public static extern void CoRevokeClassObject(uint dwRegister);
  123. static Guid ProgID_IPassword = new Guid("{834C5A62-E0BB-4FB4-87B9-F37C869C976B}");
  124. static Guid IID_IClassFactory = typeof(IClassFactory).GUID;
  125. static Guid IID_IUnknown = new Guid("{00000000-0000-0000-C000-000000000046}");
  126. public static object CreatePassword() {
  127. object classObject;
  128. DllGetClassObject(ref ProgID_IPassword, ref IID_IClassFactory, out classObject);
  129. uint dwRegister;
  130. CoRegisterClassObject(ref IID_IClassFactory, classObject, 0, 0, out dwRegister);
  131. IClassFactory classFactory = classObject as IClassFactory;
  132. object password = classFactory.CreateInstance(null, ref IID_IUnknown);
  133. return password;
  134. }
  135. }
  136. }
  137. #endif