Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.93 KB | None | 0 0
  1. //Subgroup WebAgent
  2. [Flags]
  3. public enum WebAgentPermission : long
  4. {
  5. [DescriptionAttribute("View Rule Group")]
  6. ViewRuleGroup = 1,
  7. [DescriptionAttribute("Add Rule Group")]
  8. AddRuleGroup = 2,
  9. [DescriptionAttribute("Edit Rule Group")]
  10. EditRuleGroup = 4,
  11. [DescriptionAttribute("Delete Rule Group")]
  12. DeleteRuleGroup = 8,
  13. [DescriptionAttribute("View Rule")]
  14. ViewRule = 16,
  15. [DescriptionAttribute("Add Rule")]
  16. AddRule = 32,
  17. [DescriptionAttribute("Edit Rule")]
  18. EditRule = 64,
  19. [DescriptionAttribute("Delete Rule")]
  20. DeleteRule = 128,
  21. [DescriptionAttribute("View Location")]
  22. ViewLocation = 256,
  23. [DescriptionAttribute("Add Location")]
  24. AddLocation = 512,
  25. [DescriptionAttribute("Edit Location")]
  26. EditLocation = 1024,
  27. [DescriptionAttribute("Delete Location")]
  28. DeleteLocation = 2048,
  29. [DescriptionAttribute("View Volume Statistics")]
  30. ViewVolumeStatistics = 4096,
  31. [DescriptionAttribute("Edit Volume Statistics")]
  32. EditVolumeStatistics = 8192,
  33. [DescriptionAttribute("Upload Volume Statistics")]
  34. UploadVolumeStatistics = 16384,
  35. [DescriptionAttribute("View Role")]
  36. ViewRole = 32768,
  37. [DescriptionAttribute("Add Role")]
  38. AddRole = 65536,
  39. [DescriptionAttribute("Edit Role")]
  40. EditRole = 131072,
  41. [DescriptionAttribute("Delete Role")]
  42. DeleteRole = 262144,
  43. [DescriptionAttribute("View User")]
  44. ViewUser = 524288,
  45. [DescriptionAttribute("Add User")]
  46. AddUser = 1048576,
  47. [DescriptionAttribute("Edit User")]
  48. EditUser = 2097152,
  49. [DescriptionAttribute("Delete User")]
  50. DeleteUser = 4194304,
  51. [DescriptionAttribute("Assign Permissions To User")]
  52. AssignPermissionsToUser = 8388608,
  53. [DescriptionAttribute("Change User Password")]
  54. ChangeUserPassword = 16777216,
  55. [DescriptionAttribute("View Audit Logs")]
  56. ViewAuditLogs = 33554432,
  57. [DescriptionAttribute("View Team")]
  58. ViewTeam = 67108864,
  59. [DescriptionAttribute("Add Team")]
  60. AddTeam = 134217728,
  61. [DescriptionAttribute("Edit Team")]
  62. EditTeam = 268435456,
  63. [DescriptionAttribute("Delete Team")]
  64. DeleteTeam = 536870912,
  65. [DescriptionAttribute("View Web Agent Reports")]
  66. ViewWebAgentReports = 1073741824,
  67. [DescriptionAttribute("View All Locations")]
  68. ViewAllLocations = 2147483648,
  69. [DescriptionAttribute("Access to My Search")]
  70. AccessToMySearch = 4294967296,
  71. [DescriptionAttribute("Access to Pespective Search")]
  72. AccessToPespectiveSearch = 8589934592,
  73. [DescriptionAttribute("Add Pespective Search")]
  74. AddPespectiveSearch = 17179869184,
  75. [DescriptionAttribute("Edit Pespective Search")]
  76. EditPespectiveSearch = 34359738368,
  77. [DescriptionAttribute("Delete Pespective Search")]
  78. DeletePespectiveSearch = 68719476736,
  79. [DescriptionAttribute("Access to Search")]
  80. AccessToSearch = 137438953472,
  81. [DescriptionAttribute("View Form Roles")]
  82. ViewFormRole = 274877906944,
  83. [DescriptionAttribute("Add / Edit Form Roles")]
  84. AddFormRole = 549755813888,
  85. [DescriptionAttribute("Delete UserFormRolesDifferenceMasks")]
  86. DeleteFormRole = 1099511627776,
  87. [DescriptionAttribute("Export Locations")]
  88. ExportLocations = 2199023255552,
  89. [DescriptionAttribute("Import Locations")]
  90. ImportLocations = 4398046511104,
  91. [DescriptionAttribute("Manage Location Levels")]
  92. ManageLocationLevels = 8796093022208,
  93. [DescriptionAttribute("View Job Title")]
  94. ViewJobTitle = 17592186044416,
  95. [DescriptionAttribute("Add Job Title")]
  96. AddJobTitle = 35184372088832,
  97. [DescriptionAttribute("Edit Job Title")]
  98. EditJobTitle = 70368744177664,
  99. [DescriptionAttribute("Delete Job Title")]
  100. DeleteJobTitle = 140737488355328,
  101. [DescriptionAttribute("View Dictionary Manager")]
  102. ViewDictionaryManager = 281474976710656,
  103. [DescriptionAttribute("Add Dictionary Manager")]
  104. AddDictionaryManager = 562949953421312,
  105. [DescriptionAttribute("Edit Dictionary Manager")]
  106. EditDictionaryManager = 1125899906842624,
  107. [DescriptionAttribute("Delete Dictionary Manager")]
  108. DeleteDictionaryManager = 2251799813685248,
  109. [DescriptionAttribute("View Choice Manager")]
  110. ViewChoiceManager = 4503599627370496,
  111. [DescriptionAttribute("Add Choice Manager")]
  112. AddChoiceManager = 9007199254740992,
  113. [DescriptionAttribute("Edit Chioce Manager")]
  114. EditChoiceManager = 18014398509481984,
  115. [DescriptionAttribute("Delete Choice Manager")]
  116. DeleteChoiceManager = 36028797018963968,
  117. [DescriptionAttribute("Import Export Choices")] //57
  118. ImportExportChoices = 72057594037927936
  119. }
  120.  
  121. [Flags]
  122. public enum Permissions
  123. {
  124. View = 1,
  125. Add = 2,
  126. Edit = 4,
  127. Delete = 8
  128. }
  129.  
  130. class WebAgentPermissions
  131. {
  132. public Permissions RuleGroup { get; set; }
  133. public Permissions Rule{ get; set; }
  134. public Permissions Location{ get; set; }
  135. // and so on...
  136. }
  137.  
  138. public enum MyEnumType : Int64
  139.  
  140. public sealed class WebAgentPermission
  141. {
  142. private long ID;
  143.  
  144. public static readonly WebAgentPermission
  145. ViewRuleGroup = new WebAgentPermission { ID = 1 };
  146. public static readonly WebAgentPermission
  147. AddRuleGroup = new WebAgentPermission { ID = 2 };
  148.  
  149. private WebAgentPermission() { }
  150.  
  151. // considerations: override equals/gethashcode, probably override tostring,
  152. // maybe implicit cast to/from long, maybe other stuff
  153. }
  154.  
  155. Permissions perms = agent.GetPermissions(ResourceType.User);
  156. if((perms & Permissions.View) == Permissions.View) { /* do work */ }
  157.  
  158. Permissions perms = agent.Permissions[ResourceType.User];
  159. if((perms & Permissions.View) == Permissions.View) { /* do work */ }
  160.  
  161. if(agent.IsAuthorized(ResourceType.User, Permissions.View)) { /* do work */ }
  162.  
  163. RuleGroupPermission
  164. None = 0
  165. ViewRuleGroup = 1,
  166. AddRuleGroup = 2,
  167. EditRuleGroup = 4,
  168. DeleteRuleGroup = 8,
  169.  
  170. LocationOperations
  171. None = 0
  172. Add = 1
  173. View = 2
  174. Delete = 4
  175.  
  176. void setPermission(RuleGroupPermission ruleGroupOpsAllowed, LocationOperations locationOptions)
  177. {
  178. ...
  179. }
  180.  
  181. #define NUMERICS
  182.  
  183. using System;
  184. using System.Collections;
  185. using System.Collections.Concurrent;
  186. using System.Collections.Generic;
  187. using System.ComponentModel;
  188. using System.Globalization;
  189. using System.Linq;
  190. #if NUMERICS
  191. using System.Numerics;
  192. #endif
  193. using System.Reflection;
  194. using System.Text;
  195. using System.Threading.Tasks;
  196.  
  197.  
  198. namespace Aim
  199. {
  200. /// <summary>
  201. /// The BigFlags struct behaves like a Flags enumerated type.
  202. /// <para>
  203. /// Note that if this struct will be stored in some type of data
  204. /// store, it should be stored as a string type. There are two
  205. /// reasons for this:
  206. /// </para>
  207. /// <para>
  208. /// 1. Presumably, this pattern is being used because the number
  209. /// of values will exceed 64 (max positions in a long flags enum).
  210. /// Since this is so, there is in any case no numeric type which
  211. /// can store all the possible combinations of flags.
  212. /// </para>
  213. /// <para>
  214. /// 2. The "enum" values are assigned based on the order that the
  215. /// static public fields are defined. It is much safer to store
  216. /// these fields by name in case the fields are rearranged. This
  217. /// is particularly important if this represents a permission set!
  218. /// </para>
  219. /// </summary>
  220. [
  221. TypeConverter( typeof( BigFlagsConverter ) )
  222. ]
  223. public struct BigFlags : IEquatable<BigFlags>,
  224. IComparable<BigFlags>, IComparable, IConvertible
  225. {
  226. #region State...
  227.  
  228. private static readonly List<FieldInfo> Fields;
  229. private static readonly List<BigFlags> FieldValues;
  230. #if NUMERICS
  231. private static readonly bool ZeroInit = true;
  232. private BigInteger Value;
  233.  
  234. /// <summary>
  235. /// Creates a value taking ZeroInit into consideration.
  236. /// </summary>
  237. /// <param name="index"></param>
  238. /// <returns></returns>
  239. private static BigInteger CreateValue( int index )
  240. {
  241. if( ZeroInit && index == 0 )
  242. {
  243. return 0;
  244. }
  245. int idx = ZeroInit ? index - 1 : index;
  246.  
  247. return new BigInteger( 1 ) << idx;
  248. }
  249. #else
  250. private BitArray Array;
  251.  
  252. /// <summary>
  253. /// Lazy-initialized BitArray.
  254. /// </summary>
  255. private BitArray Bits
  256. {
  257. get
  258. {
  259. if( null == Array )
  260. {
  261. Array = new BitArray( Fields.Count );
  262. }
  263. return Array;
  264. }
  265. }
  266. #endif
  267. #endregion ...State
  268.  
  269. #region Construction...
  270.  
  271. /// <summary>
  272. /// Static constructor. Sets the static public fields.
  273. /// </summary>
  274. static BigFlags()
  275. {
  276. Fields = typeof( BigFlags ).GetFields(
  277. BindingFlags.Public | BindingFlags.Static ).ToList();
  278. FieldValues = new List<BigFlags>();
  279. for( int i = 0; i < Fields.Count; i++ )
  280. {
  281. var field = Fields[i];
  282. var fieldVal = new BigFlags();
  283. #if NUMERICS
  284. fieldVal.Value = CreateValue( i );
  285. #else
  286. fieldVal.Bits.Set( i, true );
  287. #endif
  288. field.SetValue( null, fieldVal );
  289. FieldValues.Add( fieldVal );
  290. }
  291. }
  292. #endregion ...Construction
  293.  
  294. #region Operators...
  295.  
  296. /// <summary>
  297. /// OR operator. Or together BigFlags instances.
  298. /// </summary>
  299. /// <param name="lhs"></param>
  300. /// <param name="rhs"></param>
  301. /// <returns></returns>
  302. public static BigFlags operator |( BigFlags lhs, BigFlags rhs )
  303. {
  304. var result = new BigFlags();
  305. #if NUMERICS
  306. result.Value = lhs.Value | rhs.Value;
  307. #else
  308. // BitArray is modified in place - always copy!
  309. result.Array = new BitArray( lhs.Bits ).Or( rhs.Bits );
  310. #endif
  311.  
  312. return result;
  313. }
  314.  
  315. /// <summary>
  316. /// AND operator. And together BigFlags instances.
  317. /// </summary>
  318. /// <param name="lhs"></param>
  319. /// <param name="rhs"></param>
  320. /// <returns></returns>
  321. public static BigFlags operator &( BigFlags lhs, BigFlags rhs )
  322. {
  323. var result = new BigFlags();
  324. #if NUMERICS
  325. result.Value = lhs.Value & rhs.Value;
  326. #else
  327. // BitArray is modified in place - always copy!
  328. result.Array = new BitArray( lhs.Bits ).And( rhs.Bits );
  329. #endif
  330.  
  331. return result;
  332. }
  333.  
  334. /// <summary>
  335. /// XOR operator. Xor together BigFlags instances.
  336. /// </summary>
  337. /// <param name="lhs"></param>
  338. /// <param name="rhs"></param>
  339. /// <returns></returns>
  340. public static BigFlags operator ^( BigFlags lhs, BigFlags rhs )
  341. {
  342. var result = new BigFlags();
  343. #if NUMERICS
  344. result.Value = lhs.Value ^ rhs.Value;
  345. #else
  346. // BitArray is modified in place - always copy!
  347. result.Array = new BitArray( lhs.Bits ).Xor( rhs.Bits );
  348. #endif
  349.  
  350. return result;
  351. }
  352.  
  353. /// <summary>
  354. /// Equality operator.
  355. /// </summary>
  356. /// <param name="lhs"></param>
  357. /// <param name="rhs"></param>
  358. /// <returns></returns>
  359. public static bool operator ==( BigFlags lhs, BigFlags rhs )
  360. {
  361. return lhs.Equals( rhs );
  362. }
  363.  
  364. /// <summary>
  365. /// Inequality operator.
  366. /// </summary>
  367. /// <param name="lhs"></param>
  368. /// <param name="rhs"></param>
  369. /// <returns></returns>
  370. public static bool operator !=( BigFlags lhs, BigFlags rhs )
  371. {
  372. return !( lhs == rhs );
  373. }
  374. #endregion ...Operators
  375.  
  376. #region System.Object Overrides...
  377.  
  378. /// <summary>
  379. /// Overridden. Returns a comma-separated string.
  380. /// </summary>
  381. /// <returns></returns>
  382. public override string ToString()
  383. {
  384. #if NUMERICS
  385. if( ZeroInit && Value == 0 )
  386. {
  387. return Fields[0].Name;
  388. }
  389. #endif
  390. var names = new List<string>();
  391. for( int i = 0; i < Fields.Count; i++ )
  392. {
  393. #if NUMERICS
  394. if( ZeroInit && i == 0 )
  395. continue;
  396.  
  397. var bi = CreateValue( i );
  398. if( ( Value & bi ) == bi )
  399. names.Add( Fields[i].Name );
  400. #else
  401. if( Bits[i] )
  402. names.Add( Fields[i].Name );
  403. #endif
  404. }
  405.  
  406. return String.Join( ", ", names );
  407. }
  408.  
  409. /// <summary>
  410. /// Overridden. Compares equality with another object.
  411. /// </summary>
  412. /// <param name="obj"></param>
  413. /// <returns></returns>
  414. public override bool Equals( object obj )
  415. {
  416. if( obj is BigFlags )
  417. {
  418. return Equals( (BigFlags)obj );
  419. }
  420.  
  421. return false;
  422. }
  423.  
  424. /// <summary>
  425. /// Overridden. Gets the hash code of the internal BitArray.
  426. /// </summary>
  427. /// <returns></returns>
  428. public override int GetHashCode()
  429. {
  430. #if NUMERICS
  431. return Value.GetHashCode();
  432. #else
  433. int hash = 17;
  434. for( int i = 0; i < Bits.Length; i++ )
  435. {
  436. if( Bits[i] )
  437. hash ^= i;
  438. }
  439.  
  440. return hash;
  441. #endif
  442. }
  443. #endregion ...System.Object Overrides
  444.  
  445. #region IEquatable<BigFlags> Members...
  446.  
  447. /// <summary>
  448. /// Strongly-typed equality method.
  449. /// </summary>
  450. /// <param name="other"></param>
  451. /// <returns></returns>
  452. public bool Equals( BigFlags other )
  453. {
  454. #if NUMERICS
  455. return Value == other.Value;
  456. #else
  457. for( int i = 0; i < Bits.Length; i++ )
  458. {
  459. if( Bits[i] != other.Bits[i] )
  460. return false;
  461. }
  462.  
  463. return true;
  464. #endif
  465. }
  466. #endregion ...IEquatable<BigFlags> Members
  467.  
  468. #region IComparable<BigFlags> Members...
  469.  
  470. /// <summary>
  471. /// Compares based on highest bit set. Instance with higher
  472. /// bit set is bigger.
  473. /// </summary>
  474. /// <param name="other"></param>
  475. /// <returns></returns>
  476. public int CompareTo( BigFlags other )
  477. {
  478. #if NUMERICS
  479. return Value.CompareTo( other.Value );
  480. #else
  481. for( int i = Bits.Length - 1; i >= 0; i-- )
  482. {
  483. bool thisVal = Bits[i];
  484. bool otherVal = other.Bits[i];
  485. if( thisVal && !otherVal )
  486. return 1;
  487. else if( !thisVal && otherVal )
  488. return -1;
  489. }
  490.  
  491. return 0;
  492. #endif
  493. }
  494. #endregion ...IComparable<BigFlags> Members
  495.  
  496. #region IComparable Members...
  497.  
  498. int IComparable.CompareTo( object obj )
  499. {
  500. if( obj is BigFlags )
  501. {
  502. return CompareTo( (BigFlags)obj );
  503. }
  504.  
  505. return -1;
  506. }
  507. #endregion ...IComparable Members
  508.  
  509. #region IConvertible Members...
  510.  
  511. /// <summary>
  512. /// Returns TypeCode.Object.
  513. /// </summary>
  514. /// <returns></returns>
  515. public TypeCode GetTypeCode()
  516. {
  517. return TypeCode.Object;
  518. }
  519.  
  520. bool IConvertible.ToBoolean( IFormatProvider provider )
  521. {
  522. throw new NotSupportedException();
  523. }
  524.  
  525. byte IConvertible.ToByte( IFormatProvider provider )
  526. {
  527. #if NUMERICS
  528. return Convert.ToByte( Value );
  529. #else
  530. throw new NotSupportedException();
  531. #endif
  532. }
  533.  
  534. char IConvertible.ToChar( IFormatProvider provider )
  535. {
  536. throw new NotSupportedException();
  537. }
  538.  
  539. DateTime IConvertible.ToDateTime( IFormatProvider provider )
  540. {
  541. throw new NotSupportedException();
  542. }
  543.  
  544. decimal IConvertible.ToDecimal( IFormatProvider provider )
  545. {
  546. #if NUMERICS
  547. return Convert.ToDecimal( Value );
  548. #else
  549. throw new NotSupportedException();
  550. #endif
  551. }
  552.  
  553. double IConvertible.ToDouble( IFormatProvider provider )
  554. {
  555. #if NUMERICS
  556. return Convert.ToDouble( Value );
  557. #else
  558. throw new NotSupportedException();
  559. #endif
  560. }
  561.  
  562. short IConvertible.ToInt16( IFormatProvider provider )
  563. {
  564. #if NUMERICS
  565. return Convert.ToInt16( Value );
  566. #else
  567. throw new NotSupportedException();
  568. #endif
  569. }
  570.  
  571. int IConvertible.ToInt32( IFormatProvider provider )
  572. {
  573. #if NUMERICS
  574. return Convert.ToInt32( Value );
  575. #else
  576. throw new NotSupportedException();
  577. #endif
  578. }
  579.  
  580. long IConvertible.ToInt64( IFormatProvider provider )
  581. {
  582. #if NUMERICS
  583. return Convert.ToInt64( Value );
  584. #else
  585. throw new NotSupportedException();
  586. #endif
  587. }
  588.  
  589. sbyte IConvertible.ToSByte( IFormatProvider provider )
  590. {
  591. #if NUMERICS
  592. return Convert.ToSByte( Value );
  593. #else
  594. throw new NotSupportedException();
  595. #endif
  596. }
  597.  
  598. float IConvertible.ToSingle( IFormatProvider provider )
  599. {
  600. #if NUMERICS
  601. return Convert.ToSingle( Value );
  602. #else
  603. throw new NotSupportedException();
  604. #endif
  605. }
  606.  
  607. string IConvertible.ToString( IFormatProvider provider )
  608. {
  609. return ToString();
  610. }
  611.  
  612. object IConvertible.ToType( Type conversionType, IFormatProvider provider )
  613. {
  614. var tc = TypeDescriptor.GetConverter( this );
  615.  
  616. return tc.ConvertTo( this, conversionType );
  617. }
  618.  
  619. ushort IConvertible.ToUInt16( IFormatProvider provider )
  620. {
  621. #if NUMERICS
  622. return Convert.ToUInt16( Value );
  623. #else
  624. throw new NotSupportedException();
  625. #endif
  626. }
  627.  
  628. uint IConvertible.ToUInt32( IFormatProvider provider )
  629. {
  630. #if NUMERICS
  631. return Convert.ToUInt32( Value );
  632. #else
  633. throw new NotSupportedException();
  634. #endif
  635. }
  636.  
  637. ulong IConvertible.ToUInt64( IFormatProvider provider )
  638. {
  639. #if NUMERICS
  640. return Convert.ToUInt64( Value );
  641. #else
  642. throw new NotSupportedException();
  643. #endif
  644. }
  645. #endregion ...IConvertible Members
  646.  
  647. #region Public Interface...
  648.  
  649. /// <summary>
  650. /// Checks <paramref name="flags"/> to see if all the bits set in
  651. /// that flags are also set in this flags.
  652. /// </summary>
  653. /// <param name="flags"></param>
  654. /// <returns></returns>
  655. public bool HasFlag( BigFlags flags )
  656. {
  657. return ( this & flags ) == flags;
  658. }
  659.  
  660. /// <summary>
  661. /// Gets the names of this BigFlags enumerated type.
  662. /// </summary>
  663. /// <returns></returns>
  664. public static string[] GetNames()
  665. {
  666. return Fields.Select( x => x.Name ).ToArray();
  667. }
  668.  
  669. /// <summary>
  670. /// Gets all the values of this BigFlags enumerated type.
  671. /// </summary>
  672. /// <returns></returns>
  673. public static BigFlags[] GetValues()
  674. {
  675. return FieldValues.ToArray();
  676. }
  677.  
  678. /// <summary>
  679. /// Standard TryParse pattern. Parses a BigFlags result from a string.
  680. /// </summary>
  681. /// <param name="s"></param>
  682. /// <param name="result"></param>
  683. /// <returns></returns>
  684. public static bool TryParse( string s, out BigFlags result )
  685. {
  686. result = new BigFlags();
  687. if( String.IsNullOrEmpty( s ) )
  688. return true;
  689.  
  690. var fieldNames = s.Split( ',' );
  691. foreach( var f in fieldNames )
  692. {
  693. var field = Fields.FirstOrDefault( x =>
  694. String.Equals( x.Name, f.Trim(),
  695. StringComparison.OrdinalIgnoreCase ) );
  696. if( null == field )
  697. {
  698. result = new BigFlags();
  699. return false;
  700. }
  701. #if NUMERICS
  702. int i = Fields.IndexOf( field );
  703. result.Value |= CreateValue( i );
  704. #else
  705. result.Bits.Set( Fields.IndexOf( field ), true );
  706. #endif
  707. }
  708.  
  709. return true;
  710. }
  711.  
  712. //
  713. // Expose "enums" as public static readonly fields.
  714. // TODO: Replace this section with your "enum" values.
  715. //
  716. public static readonly BigFlags None;
  717. public static readonly BigFlags FirstValue;
  718. public static readonly BigFlags ValueTwo;
  719. public static readonly BigFlags ValueThree;
  720. public static readonly BigFlags ValueFour;
  721. public static readonly BigFlags ValueFive;
  722. public static readonly BigFlags ValueSix;
  723. public static readonly BigFlags LastValue;
  724.  
  725. /// <summary>
  726. /// Expose flagged combinations as get-only properties.
  727. /// </summary>
  728. public static BigFlags FirstLast
  729. {
  730. get
  731. {
  732. return BigFlags.FirstValue | BigFlags.LastValue;
  733. }
  734. }
  735. #endregion ...Public Interface
  736. }
  737.  
  738. /// <summary>
  739. /// Converts objects to and from BigFlags instances.
  740. /// </summary>
  741. public class BigFlagsConverter : TypeConverter
  742. {
  743. /// <summary>
  744. /// Can convert to string only.
  745. /// </summary>
  746. /// <param name="context"></param>
  747. /// <param name="destinationType"></param>
  748. /// <returns></returns>
  749. public override bool CanConvertTo( ITypeDescriptorContext context,
  750. Type destinationType )
  751. {
  752. return destinationType == typeof( String );
  753. }
  754.  
  755. /// <summary>
  756. /// Can convert from any object.
  757. /// </summary>
  758. /// <param name="context"></param>
  759. /// <param name="sourceType"></param>
  760. /// <returns></returns>
  761. public override bool CanConvertFrom( ITypeDescriptorContext context,
  762. Type sourceType )
  763. {
  764. return true;
  765. }
  766.  
  767. /// <summary>
  768. /// Converts BigFlags to a string.
  769. /// </summary>
  770. /// <param name="context"></param>
  771. /// <param name="culture"></param>
  772. /// <param name="value"></param>
  773. /// <param name="destinationType"></param>
  774. /// <returns></returns>
  775. public override object ConvertTo( ITypeDescriptorContext context,
  776. CultureInfo culture, object value, Type destinationType )
  777. {
  778. if( value is BigFlags && CanConvertTo( destinationType ) )
  779. return value.ToString();
  780.  
  781. return null;
  782. }
  783.  
  784. /// <summary>
  785. /// Attempts to parse <paramref name="value"/> and create and
  786. /// return a new BigFlags instance.
  787. /// </summary>
  788. /// <param name="context"></param>
  789. /// <param name="culture"></param>
  790. /// <param name="value"></param>
  791. /// <returns></returns>
  792. public override object ConvertFrom( ITypeDescriptorContext context,
  793. CultureInfo culture, object value )
  794. {
  795. var s = Convert.ToString( value );
  796. BigFlags result;
  797. BigFlags.TryParse( s, out result );
  798.  
  799. return result;
  800. }
  801. }
  802. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement