1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
|
// ---------------------------------------------------------------------------
// Copyright (C) 2005 Microsoft Corporation All Rights Reserved
// ---------------------------------------------------------------------------
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Globalization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using System.Reflection;
using System.Workflow.Activities.Common;
namespace System.Workflow.Activities.Rules
{
public enum RuleAttributeTarget
{
Parameter,
This
}
public abstract class RuleAttribute : Attribute
{
internal abstract bool Validate(RuleValidation validation, MemberInfo member, Type contextType, ParameterInfo[] parameters);
internal abstract void Analyze(RuleAnalysis analysis, MemberInfo member, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List<CodeExpression> attributedExpressions);
}
public abstract class RuleReadWriteAttribute : RuleAttribute
{
private RuleAttributeTarget attributeTarget;
private string attributePath;
protected RuleReadWriteAttribute(string path, RuleAttributeTarget target)
{
this.attributeTarget = target;
this.attributePath = path;
}
public string Path
{
get { return attributePath; }
}
public RuleAttributeTarget Target
{
get { return attributeTarget; }
}
internal override bool Validate(RuleValidation validation, MemberInfo member, Type contextType, ParameterInfo[] parameters)
{
ValidationError error = null;
string message = null;
if (string.IsNullOrEmpty(attributePath))
{
// It is allowed to pass null or the empty string to [RuleRead] or [RuleWrite]. This
// is how you indicate that a method or property has no dependencies or side effects.
return true;
}
bool valid = true;
string[] parts = attributePath.Split('/');
// Check the first part.
string firstPart = parts[0];
int startOfRelativePortion = 0;
if (attributeTarget == RuleAttributeTarget.This)
{
// When target is "This", the path is allowed to start with the token "this". It is
// then skipped for the rest of the validation, and the contextType remains what it
// was when passed in.
if (firstPart == "this")
++startOfRelativePortion;
}
else
{
// When target is "Parameter", the path must start with the name of a parameter.
bool found = false;
for (int p = 0; p < parameters.Length; ++p)
{
ParameterInfo param = parameters[p];
if (param.Name == firstPart)
{
found = true;
// The context type is the parameter type.
contextType = param.ParameterType;
break;
}
}
if (!found)
{
message = string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleAttributeParameter, firstPart, member.Name);
error = new ValidationError(message, ErrorNumbers.Error_InvalidRuleAttributeParameter);
error.UserData[RuleUserDataKeys.ErrorObject] = this;
validation.AddError(error);
return false;
}
++startOfRelativePortion;
}
int numParts = parts.Length;
// Check the last part. The last part is allowed to be empty, or "*".
string lastPart = parts[numParts - 1];
if (string.IsNullOrEmpty(lastPart) || lastPart == "*")
numParts -= 1;
// Check the rest of the parts.
Type currentType = contextType;
for (int i = startOfRelativePortion; i < numParts; ++i)
{
// Can't have embedded "*" wildcards.
if (parts[i] == "*")
{
// The "*" occurred in the middle of the path, which is a no-no.
error = new ValidationError(Messages.InvalidWildCardInPathQualifier, ErrorNumbers.Error_InvalidWildCardInPathQualifier);
error.UserData[RuleUserDataKeys.ErrorObject] = this;
validation.AddError(error);
valid = false;
break;
}
// Skip array types.
while (currentType.IsArray)
currentType = currentType.GetElementType();
// Make sure the member exists in the current type.
BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy;
if (validation.AllowInternalMembers(currentType))
bindingFlags |= BindingFlags.NonPublic;
FieldInfo field = currentType.GetField(parts[i], bindingFlags);
if (field != null)
{
currentType = field.FieldType;
}
else
{
PropertyInfo property = currentType.GetProperty(parts[i], bindingFlags);
if (property != null)
{
currentType = property.PropertyType;
}
else
{
message = string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, parts[i]);
error = new ValidationError(message, ErrorNumbers.Error_UnknownFieldOrProperty);
error.UserData[RuleUserDataKeys.ErrorObject] = this;
validation.AddError(error);
valid = false;
break;
}
}
}
return valid;
}
internal void AnalyzeReadWrite(RuleAnalysis analysis, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List<CodeExpression> attributedExpressions)
{
if (string.IsNullOrEmpty(attributePath))
{
// If the suffix is null or empty, this means the RuleAttributeTarget has no dependencies.
if (attributeTarget == RuleAttributeTarget.This)
{
// The target object has no dependencies.
attributedExpressions.Add(targetExpression);
}
else if (attributeTarget == RuleAttributeTarget.Parameter)
{
// ALL arguments have no dependencies.
for (int i = 0; i < argumentExpressions.Count; ++i)
attributedExpressions.Add(argumentExpressions[i]);
}
}
else
{
string suffix = attributePath;
bool isRead = !analysis.ForWrites;
bool isWrite = analysis.ForWrites;
if (attributeTarget == RuleAttributeTarget.This)
{
// Target is "This", so perform the analysis on the target expression.
// Remove the optional "this/" token if present.
string optionalPrefix = "this/";
if (suffix.StartsWith(optionalPrefix, StringComparison.Ordinal))
suffix = suffix.Substring(optionalPrefix.Length);
RuleExpressionWalker.AnalyzeUsage(analysis, targetExpression, isRead, isWrite, new RulePathQualifier(suffix, targetQualifier));
attributedExpressions.Add(targetExpression);
}
else if (attributeTarget == RuleAttributeTarget.Parameter)
{
string paramName = null;
int firstSlash = suffix.IndexOf('/');
if (firstSlash >= 0)
{
paramName = suffix.Substring(0, firstSlash);
suffix = suffix.Substring(firstSlash + 1);
}
else
{
paramName = suffix;
suffix = null;
}
// Find the ParameterInfo that corresponds to this attribute path.
ParameterInfo param = Array.Find<ParameterInfo>(parameters,
delegate(ParameterInfo p) { return p.Name == paramName; });
if (param != null)
{
RulePathQualifier qualifier = string.IsNullOrEmpty(suffix) ? null : new RulePathQualifier(suffix, null);
// 99.9% of the time, the parameter usage attribute only applies to one argument. However,
// if this attribute corresponds to the last parameter, then just assume that all the trailing
// arguments correspond. (In other words, if the caller passed more arguments then there
// are parameters, we assume it was a params array.)
//
// Usually this loop will only execute once.
int end = param.Position + 1;
if (param.Position == parameters.Length - 1)
end = argumentExpressions.Count;
for (int i = param.Position; i < end; ++i)
{
CodeExpression argExpr = argumentExpressions[i];
RuleExpressionWalker.AnalyzeUsage(analysis, argExpr, isRead, isWrite, qualifier);
attributedExpressions.Add(argExpr);
}
}
}
}
}
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = true)]
public sealed class RuleReadAttribute : RuleReadWriteAttribute
{
public RuleReadAttribute(string path, RuleAttributeTarget target)
: base(path, target)
{
}
public RuleReadAttribute(string path)
: base(path, RuleAttributeTarget.This)
{
}
internal override void Analyze(RuleAnalysis analysis, MemberInfo member, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List<CodeExpression> attributedExpressions)
{
// A RuleRead attribute is only applicable if we're analyzing for reads.
if (analysis.ForWrites)
return;
base.AnalyzeReadWrite(analysis, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
}
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = true)]
public sealed class RuleWriteAttribute : RuleReadWriteAttribute
{
public RuleWriteAttribute(string path, RuleAttributeTarget target)
: base(path, target)
{
}
public RuleWriteAttribute(string path)
: base(path, RuleAttributeTarget.This)
{
}
internal override void Analyze(RuleAnalysis analysis, MemberInfo member, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List<CodeExpression> attributedExpressions)
{
// A RuleWrite attribute is only applicable if we're analyzing for writes.
if (!analysis.ForWrites)
return;
base.AnalyzeReadWrite(analysis, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
}
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = true)]
public sealed class RuleInvokeAttribute : RuleAttribute
{
private string methodInvoked;
public RuleInvokeAttribute(string methodInvoked)
{
this.methodInvoked = methodInvoked;
}
public string MethodInvoked
{
get { return methodInvoked; }
}
internal override bool Validate(RuleValidation validation, MemberInfo member, Type contextType, ParameterInfo[] parameters)
{
Stack<MemberInfo> methodStack = new Stack<MemberInfo>();
methodStack.Push(member);
bool result = ValidateInvokeAttribute(validation, member, contextType, methodStack);
methodStack.Pop();
return result;
}
private bool ValidateInvokeAttribute(RuleValidation validation, MemberInfo member, Type contextType, Stack<MemberInfo> methodStack)
{
string message;
ValidationError error;
if (string.IsNullOrEmpty(methodInvoked))
{
// Invoked method or property name was null or empty.
message = string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, member.Name, this.GetType().Name, Messages.NullValue);
error = new ValidationError(message, ErrorNumbers.Warning_RuleAttributeNoMatch, true);
error.UserData[RuleUserDataKeys.ErrorObject] = this;
validation.AddError(error);
return false;
}
bool valid = true;
// Go through all the methods and properties on the target context,
// looking for all the ones that match the name on the attribute.
MemberInfo[] members = contextType.GetMember(methodInvoked, MemberTypes.Method | MemberTypes.Property, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
if (members == null || members.Length == 0)
{
// Invoked method or property didn't exist.
message = string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, member.Name, this.GetType().Name, methodInvoked);
error = new ValidationError(message, ErrorNumbers.Warning_RuleAttributeNoMatch, true);
error.UserData[RuleUserDataKeys.ErrorObject] = this;
validation.AddError(error);
valid = false;
}
else
{
for (int i = 0; i < members.Length; ++i)
{
MemberInfo mi = members[i];
if (!methodStack.Contains(mi)) // Prevent recursion
{
methodStack.Push(mi);
object[] attrs = mi.GetCustomAttributes(typeof(RuleAttribute), true);
if (attrs != null && attrs.Length != 0)
{
foreach (RuleAttribute invokedRuleAttr in attrs)
{
RuleReadWriteAttribute readWriteAttr = invokedRuleAttr as RuleReadWriteAttribute;
if (readWriteAttr != null)
{
// This read/write attribute may not specify a target of "Parameter", since
// we can't map from the invoker's parameters to the invokee's parameters.
if (readWriteAttr.Target == RuleAttributeTarget.Parameter)
{
message = string.Format(CultureInfo.CurrentCulture, Messages.InvokeAttrRefersToParameterAttribute, mi.Name);
error = new ValidationError(message, ErrorNumbers.Error_InvokeAttrRefersToParameterAttribute, true);
error.UserData[RuleUserDataKeys.ErrorObject] = this;
validation.AddError(error);
valid = false;
}
else
{
// Validate the read/write attribute normally.
readWriteAttr.Validate(validation, mi, contextType, null);
}
}
else
{
RuleInvokeAttribute invokeAttr = (RuleInvokeAttribute)invokedRuleAttr;
invokeAttr.ValidateInvokeAttribute(validation, mi, contextType, methodStack);
}
}
}
methodStack.Pop();
}
}
}
return valid;
}
internal override void Analyze(RuleAnalysis analysis, MemberInfo member, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List<CodeExpression> attributedExpressions)
{
Stack<MemberInfo> methodStack = new Stack<MemberInfo>();
methodStack.Push(member);
AnalyzeInvokeAttribute(analysis, member.DeclaringType, methodStack, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
methodStack.Pop();
}
private void AnalyzeInvokeAttribute(RuleAnalysis analysis, Type contextType, Stack<MemberInfo> methodStack, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List<CodeExpression> attributedExpressions)
{
// Go through all the methods and properties on the target context,
// looking for all the ones that match the name on the attribute.
MemberInfo[] members = contextType.GetMember(methodInvoked, MemberTypes.Method | MemberTypes.Property, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
for (int m = 0; m < members.Length; ++m)
{
MemberInfo mi = members[m];
if (!methodStack.Contains(mi)) // Prevent recursion
{
methodStack.Push(mi);
object[] attrs = mi.GetCustomAttributes(typeof(RuleAttribute), true);
if (attrs != null && attrs.Length != 0)
{
RuleAttribute[] ruleAttrs = (RuleAttribute[])attrs;
for (int i = 0; i < ruleAttrs.Length; ++i)
{
RuleAttribute ruleAttr = ruleAttrs[i];
RuleReadWriteAttribute readWriteAttr = ruleAttr as RuleReadWriteAttribute;
if (readWriteAttr != null)
{
// Just analyze the read/write attribute normally.
readWriteAttr.Analyze(analysis, mi, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
}
else
{
RuleInvokeAttribute invokeAttr = (RuleInvokeAttribute)ruleAttr;
invokeAttr.AnalyzeInvokeAttribute(analysis, contextType, methodStack, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
}
}
}
methodStack.Pop();
}
}
}
}
}
|