mscorlib(4.0.0.0) API with additions
StrongNameMembershipCondition.cs
4 using System.Security.Util;
5 
7 {
10  [ComVisible(true)]
11  public sealed class StrongNameMembershipCondition : IMembershipCondition, ISecurityEncodable, ISecurityPolicyEncodable, IConstantMembershipCondition, IReportMatchMembershipCondition
12  {
13  private StrongNamePublicKeyBlob m_publicKeyBlob;
14 
15  private string m_name;
16 
17  private Version m_version;
18 
19  private SecurityElement m_element;
20 
21  private const string s_tagName = "Name";
22 
23  private const string s_tagVersion = "AssemblyVersion";
24 
25  private const string s_tagPublicKeyBlob = "PublicKeyBlob";
26 
31  {
32  get
33  {
34  if (m_publicKeyBlob == null && m_element != null)
35  {
36  ParseKeyBlob();
37  }
38  return m_publicKeyBlob;
39  }
40  set
41  {
42  if (value == null)
43  {
44  throw new ArgumentNullException("PublicKey");
45  }
46  m_publicKeyBlob = value;
47  }
48  }
49 
53  public string Name
54  {
55  get
56  {
57  if (m_name == null && m_element != null)
58  {
59  ParseName();
60  }
61  return m_name;
62  }
63  set
64  {
65  if (value == null)
66  {
67  if (m_publicKeyBlob == null && m_element != null)
68  {
69  ParseKeyBlob();
70  }
71  if ((object)m_version == null && m_element != null)
72  {
73  ParseVersion();
74  }
75  m_element = null;
76  }
77  else if (value.Length == 0)
78  {
79  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"));
80  }
81  m_name = value;
82  }
83  }
84 
87  public Version Version
88  {
89  get
90  {
91  if ((object)m_version == null && m_element != null)
92  {
93  ParseVersion();
94  }
95  return m_version;
96  }
97  set
98  {
99  if (value == null)
100  {
101  if (m_name == null && m_element != null)
102  {
103  ParseName();
104  }
105  if (m_publicKeyBlob == null && m_element != null)
106  {
107  ParseKeyBlob();
108  }
109  m_element = null;
110  }
111  m_version = value;
112  }
113  }
114 
116  {
117  }
118 
126  {
127  if (blob == null)
128  {
129  throw new ArgumentNullException("blob");
130  }
131  if (name != null && name.Equals(""))
132  {
133  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyStrongName"));
134  }
135  m_publicKeyBlob = blob;
136  m_name = name;
137  m_version = version;
138  }
139 
144  public bool Check(Evidence evidence)
145  {
146  object usedEvidence = null;
147  return ((IReportMatchMembershipCondition)this).Check(evidence, out usedEvidence);
148  }
149 
150  bool IReportMatchMembershipCondition.Check(Evidence evidence, out object usedEvidence)
151  {
152  usedEvidence = null;
153  if (evidence == null)
154  {
155  return false;
156  }
157  StrongName delayEvaluatedHostEvidence = evidence.GetDelayEvaluatedHostEvidence<StrongName>();
158  if (delayEvaluatedHostEvidence != null)
159  {
160  bool flag = PublicKey != null && PublicKey.Equals(delayEvaluatedHostEvidence.PublicKey);
161  bool flag2 = Name == null || (delayEvaluatedHostEvidence.Name != null && StrongName.CompareNames(delayEvaluatedHostEvidence.Name, Name));
162  bool flag3 = (object)Version == null || ((object)delayEvaluatedHostEvidence.Version != null && delayEvaluatedHostEvidence.Version.CompareTo(Version) == 0);
163  if ((flag && flag2) & flag3)
164  {
165  usedEvidence = delayEvaluatedHostEvidence;
166  return true;
167  }
168  }
169  return false;
170  }
171 
175  {
177  }
178 
182  {
183  return ToXml(null);
184  }
185 
188  public void FromXml(SecurityElement e)
189  {
190  FromXml(e, null);
191  }
192 
197  {
198  SecurityElement securityElement = new SecurityElement("IMembershipCondition");
199  XMLUtil.AddClassAttribute(securityElement, GetType(), "System.Security.Policy.StrongNameMembershipCondition");
200  securityElement.AddAttribute("version", "1");
201  if (PublicKey != null)
202  {
203  securityElement.AddAttribute("PublicKeyBlob", Hex.EncodeHexString(PublicKey.PublicKey));
204  }
205  if (Name != null)
206  {
207  securityElement.AddAttribute("Name", Name);
208  }
209  if ((object)Version != null)
210  {
211  securityElement.AddAttribute("AssemblyVersion", Version.ToString());
212  }
213  return securityElement;
214  }
215 
221  public void FromXml(SecurityElement e, PolicyLevel level)
222  {
223  if (e == null)
224  {
225  throw new ArgumentNullException("e");
226  }
227  if (!e.Tag.Equals("IMembershipCondition"))
228  {
229  throw new ArgumentException(Environment.GetResourceString("Argument_MembershipConditionElement"));
230  }
231  lock (this)
232  {
233  m_name = null;
234  m_publicKeyBlob = null;
235  m_version = null;
236  m_element = e;
237  }
238  }
239 
240  private void ParseName()
241  {
242  lock (this)
243  {
244  if (m_element != null)
245  {
246  string text = m_element.Attribute("Name");
247  m_name = ((text == null) ? null : text);
248  if ((object)m_version != null && m_name != null && m_publicKeyBlob != null)
249  {
250  m_element = null;
251  }
252  }
253  }
254  }
255 
256  private void ParseKeyBlob()
257  {
258  lock (this)
259  {
260  if (m_element != null)
261  {
262  string text = m_element.Attribute("PublicKeyBlob");
263  StrongNamePublicKeyBlob strongNamePublicKeyBlob = new StrongNamePublicKeyBlob();
264  if (text == null)
265  {
266  throw new ArgumentException(Environment.GetResourceString("Argument_BlobCannotBeNull"));
267  }
268  strongNamePublicKeyBlob.PublicKey = Hex.DecodeHexString(text);
269  m_publicKeyBlob = strongNamePublicKeyBlob;
270  if ((object)m_version != null && m_name != null && m_publicKeyBlob != null)
271  {
272  m_element = null;
273  }
274  }
275  }
276  }
277 
278  private void ParseVersion()
279  {
280  lock (this)
281  {
282  if (m_element != null)
283  {
284  string text = m_element.Attribute("AssemblyVersion");
285  m_version = ((text == null) ? null : new Version(text));
286  if ((object)m_version != null && m_name != null && m_publicKeyBlob != null)
287  {
288  m_element = null;
289  }
290  }
291  }
292  }
293 
296  public override string ToString()
297  {
298  string arg = "";
299  string arg2 = "";
300  if (Name != null)
301  {
302  arg = " " + string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("StrongName_Name"), Name);
303  }
304  if ((object)Version != null)
305  {
306  arg2 = " " + string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("StrongName_Version"), Version);
307  }
308  return string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("StrongName_ToString"), Hex.EncodeHexString(PublicKey.PublicKey), arg, arg2);
309  }
310 
316  public override bool Equals(object o)
317  {
318  StrongNameMembershipCondition strongNameMembershipCondition = o as StrongNameMembershipCondition;
319  if (strongNameMembershipCondition != null)
320  {
321  if (m_publicKeyBlob == null && m_element != null)
322  {
323  ParseKeyBlob();
324  }
325  if (strongNameMembershipCondition.m_publicKeyBlob == null && strongNameMembershipCondition.m_element != null)
326  {
327  strongNameMembershipCondition.ParseKeyBlob();
328  }
329  if (object.Equals(m_publicKeyBlob, strongNameMembershipCondition.m_publicKeyBlob))
330  {
331  if (m_name == null && m_element != null)
332  {
333  ParseName();
334  }
335  if (strongNameMembershipCondition.m_name == null && strongNameMembershipCondition.m_element != null)
336  {
337  strongNameMembershipCondition.ParseName();
338  }
339  if (object.Equals(m_name, strongNameMembershipCondition.m_name))
340  {
341  if (m_version == null && m_element != null)
342  {
343  ParseVersion();
344  }
345  if (strongNameMembershipCondition.m_version == null && strongNameMembershipCondition.m_element != null)
346  {
347  strongNameMembershipCondition.ParseVersion();
348  }
349  if (object.Equals(m_version, strongNameMembershipCondition.m_version))
350  {
351  return true;
352  }
353  }
354  }
355  }
356  return false;
357  }
358 
362  public override int GetHashCode()
363  {
364  if (m_publicKeyBlob == null && m_element != null)
365  {
366  ParseKeyBlob();
367  }
368  if (m_publicKeyBlob != null)
369  {
370  return m_publicKeyBlob.GetHashCode();
371  }
372  if (m_name == null && m_element != null)
373  {
374  ParseName();
375  }
376  if (m_version == null && m_element != null)
377  {
378  ParseVersion();
379  }
380  if (m_name != null || m_version != null)
381  {
382  return ((m_name != null) ? m_name.GetHashCode() : 0) + ((!(m_version == null)) ? m_version.GetHashCode() : 0);
383  }
384  return typeof(StrongNameMembershipCondition).GetHashCode();
385  }
386  }
387 }
override int GetHashCode()
Returns a hash code for the current T:System.Version object.
Definition: Version.cs:425
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
string Name
Gets or sets the simple name of the T:System.Security.Policy.StrongName for which the membership cond...
override int GetHashCode()
Returns the hash code for the current T:System.Security.Policy.StrongNameMembershipCondition.
Represents the security policy levels for the common language runtime. This class cannot be inherited...
Definition: PolicyLevel.cs:15
Definition: __Canon.cs:3
SecurityElement ToXml()
Creates an XML encoding of the security object and its current state.
Determines whether an assembly belongs to a code group by testing its strong name....
string Tag
Gets or sets the tag name of an XML element.
IMembershipCondition Copy()
Creates an equivalent copy of the current T:System.Security.Policy.StrongNameMembershipCondition.
override string ToString()
Converts the value of the current T:System.Version object to its equivalent T:System....
Definition: Version.cs:437
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
bool Check(Evidence evidence)
Determines whether the specified evidence satisfies the membership condition.
SecurityElement ToXml(PolicyLevel level)
Creates an XML encoding of the security object and its current state with the specified T:System....
Defines the test to determine whether a code assembly is a member of a code group.
override string ToString()
Creates and returns a string representation of the current T:System.Security.Policy....
void FromXml(SecurityElement e)
Reconstructs a security object with a specified state from an XML encoding.
StrongNamePublicKeyBlob PublicKey
Gets or sets the T:System.Security.Permissions.StrongNamePublicKeyBlob of the T:System....
Represents the XML object model for encoding security objects. This class cannot be inherited.
Defines the methods that convert permission object state to and from XML element representation.
Represents the version number of an assembly, operating system, or the common language runtime....
Definition: Version.cs:11
StrongNameMembershipCondition(StrongNamePublicKeyBlob blob, string name, Version version)
Initializes a new instance of the T:System.Security.Policy.StrongNameMembershipCondition class with t...
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
The exception that is thrown when one of the arguments provided to a method is not valid.
Supports the methods that convert permission object state to and from an XML element representation.
Defines the set of information that constitutes input to security policy decisions....
Definition: Evidence.cs:17
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
Specifies that the class can be serialized.
void FromXml(SecurityElement e, PolicyLevel level)
Reconstructs a security object with a specified state from an XML encoding.
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Version Version
Gets or sets the T:System.Version of the T:System.Security.Policy.StrongName for which the membership...
override bool Equals(object o)
Determines whether the T:System.Security.Policy.StrongName from the specified object is equivalent to...
Represents the public key information (called a blob) for a strong name. This class cannot be inherit...