/// <summary> /// Get import target description where given attribute is defined /// </summary> /// <param name="attribute">Import attribute</param> /// <param name="componentType">Type of defining component</param> /// <param name="importMethodID">Id of method that can be used for import. It is <c>null</c> for self exports</param> /// <param name="importType">Type of defined export</param> /// <returns><c>true</c> if target has been successfully found, <c>false</c> otherwise</returns> private bool getImportTarget(CodeAttribute2 attribute, TypeDescriptor componentType, out MethodID importMethodID, out TypeDescriptor importType) { var target = attribute.Parent as CodeElement; importMethodID = null; importType = null; var name = target.Name(); switch (target.Kind) { case vsCMElement.vsCMElementVariable: //variables are represented by properties within type system importType = _assembly.InfoBuilder.CreateDescriptor((target as CodeVariable).Type); importMethodID = Naming.Method(componentType, Naming.SetterPrefix + name, false, ParameterTypeInfo.Create("value", importType) ); return(true); case vsCMElement.vsCMElementProperty: importType = _assembly.InfoBuilder.CreateDescriptor((target as CodeProperty).Type); importMethodID = Naming.Method(componentType, Naming.SetterPrefix + name, false, ParameterTypeInfo.Create("value", importType) ); return(true); default: return(false); } }
/// <summary> /// Get export target description where given attribute is defined /// </summary> /// <param name="attribute">Export attribute</param> /// <param name="componentType">Type of defining component</param> /// <param name="exportMethodID">Id of method that can be used for export. It is <c>null</c> for self exports</param> /// <param name="exportType">Type of defined export</param> /// <returns><c>true</c> if target has been successfully found, <c>false</c> otherwise</returns> private bool getExportTarget(CodeAttribute2 attribute, TypeDescriptor componentType, out MethodID exportMethodID, out TypeDescriptor exportType) { var target = attribute.Parent as CodeElement; exportMethodID = null; exportType = null; var name = target.Name(); switch (target.Kind) { case vsCMElement.vsCMElementVariable: //variables are represented by properties within type system exportMethodID = Naming.Method(componentType, Naming.GetterPrefix + name, false, ParameterTypeInfo.NoParams); exportType = _assembly.InfoBuilder.CreateDescriptor((target as CodeVariable).Type); return(true); case vsCMElement.vsCMElementProperty: exportMethodID = Naming.Method(componentType, Naming.GetterPrefix + name, false, ParameterTypeInfo.NoParams); exportType = _assembly.InfoBuilder.CreateDescriptor((target as CodeProperty).Type); return(true); case vsCMElement.vsCMElementClass: //self export doesnt need exportMethodID exportType = _assembly.InfoBuilder.CreateDescriptor(target as CodeClass); return(true); default: return(false); } }
/// <inheritdoc /> protected override IEnumerable <KeyValuePair <StaticPropertyWrapper, ConstantValue> > GetAttributePropertyArguments( StaticAttributeWrapper attribute) { CodeAttribute2 attributeHandle = (CodeAttribute2)attribute.Handle; throw new NotImplementedException(); }
/// <inheritdoc /> public override void VisitAttribute(CodeAttribute2 e) { var fullname = e.SafeFullname(); if ( fullname == Naming.ExportAttribute || fullname == Naming.InheritedExportAttribute ) { addExport(new AttributeInfo(e)); } else if (fullname == Naming.ImportAttribute) { addImport(new AttributeInfo(e)); } else if (fullname == Naming.ImportManyAttribute) { addImport(new AttributeInfo(e), true); } else if (fullname == Naming.CompositionPointAttribute) { addCompositionPoint(new AttributeInfo(e)); } else if (fullname == Naming.ImportingConstructorAttribute) { addImportingConstructor(new AttributeInfo(e)); } }
public static void PrintAttribute2Info(CodeAttribute2 item) { return; Debug.WriteLine("--------PrintFunction2Info--------"); Debug.WriteLine("Rename Method.FullName:" + item.FullName); Debug.WriteLine("-----------------------------------"); }
public static IEnumerable <CodeAttributeArgument> GetArguments(this CodeAttribute2 cattr) { if (cattr == null) { return(Enumerable.Empty <CodeAttributeArgument>()); } return(cattr.Arguments.Cast <CodeAttributeArgument>()); }
private string GetStringArgumentValue(CodeAttribute2 codeAttribute, string argumentName) { var arg = codeAttribute.Arguments.Cast<CodeAttributeArgument>().FirstOrDefault(a => a.Name == argumentName); if (arg == null) return null; return VsxHelper.ParseCodeStringValue(arg.Value, arg.Language); }
private CodeDomAttributeMetadata(CodeAttribute2 codeAttribute) { this.codeAttribute = codeAttribute; this.value = codeAttribute.Value; if (string.IsNullOrEmpty(this.value)) this.value = null; }
public void Attributes_ClassHasOneAttribute_ReturnsOneAttribute() { CreateClass("[System.ObsoleteAttribute] class MyClass {}"); CodeAttribute2 attribute = codeClass.Attributes.FirstCodeAttribute2OrDefault(); Assert.AreEqual(1, codeClass.Attributes.Count); Assert.AreEqual("Obsolete", attribute.Name); }
private BindingSourceAttribute CreateAttribute(CodeAttribute2 attribute) { return(new BindingSourceAttribute { AttributeType = CreateBindingType(attribute.FullName), AttributeValues = attribute.Arguments.Cast <CodeAttributeArgument>().Where(arg => string.IsNullOrEmpty(arg.Name)).Select(CreateAttributeValue).ToArray(), NamedAttributeValues = attribute.Arguments.Cast <CodeAttributeArgument>().Where(arg => !string.IsNullOrEmpty(arg.Name)).ToDictionary(na => na.Name, CreateAttributeValue) }); }
private bool IsScopeAttribute(CodeAttribute2 codeAttribute) { return (codeAttribute.FullName.Equals(typeof(ScopeAttribute).FullName) || #pragma warning disable 612,618 codeAttribute.FullName.Equals(typeof(StepScopeAttribute).FullName)); #pragma warning restore 612,618 }
void CreateAttribute(string code) { AddCodeFile("attr.cs", code + " class ClassWithAttribute {}"); var compilation = projectContent.CreateCompilation(); var testClass = compilation.FindType(new FullTypeName("ClassWithAttribute")).GetDefinition(); var attribute = testClass.Attributes.Single(); codeAttribute = new CodeAttribute2(codeModelContext, attribute); }
public void Item_GetItemByNameWhenClassHasOneAttribute_ReturnsOneAttribute() { CreateMSBuildClass(); AddAttributeToClass("TestAttribute"); CreateCodeAttributes(); CodeAttribute2 attribute = attributes.Item("Test") as CodeAttribute2; Assert.AreEqual("Test", attribute.Name); }
/// <summary> /// 获取特性参数信息 /// </summary> /// <param name="attr"></param> /// <returns></returns> public static List <CodeAttributeArgument> GetCodeAttrArgs(CodeAttribute2 attr) { List <CodeAttributeArgument> list = new List <CodeAttributeArgument>(); foreach (CodeAttributeArgument attrArg in attr.Arguments) { list.Add(attrArg); } return(list); }
private CodeDomAttributeMetadata(CodeAttribute2 codeAttribute) { this.codeAttribute = codeAttribute; this.value = codeAttribute.Value;//.Trim('"'); if (string.IsNullOrEmpty(this.value)) { this.value = null; } }
/// <summary> /// Visit given element. /// </summary> /// <param name="e">Element to visit.</param> public virtual void VisitAttribute(CodeAttribute2 e) { if (!RecursiveVisit) { //stop recursion visitUnhandled(e); return; } //There is no default deeper traversing }
private bool IsGeneralStepDefinition(CodeAttribute2 codeAttribute) { try { return(codeAttribute.FullName.Equals(typeof(StepDefinitionAttribute).FullName)); } catch (Exception) { return(false); } }
public void GetEnumerator_ClassHasOneAttribute_ReturnsOneAttribute() { CreateMSBuildClass(); AddAttributeToClass("TestAttribute"); CreateCodeAttributes(); CodeAttribute2 attribute = attributes.FirstCodeAttribute2OrDefault(); Assert.AreEqual(1, attributes.Count); Assert.AreEqual("Test", attribute.Name); }
private string GetStringArgumentValue(CodeAttribute2 codeAttribute, string argumentName) { var arg = codeAttribute.Arguments.Cast <CodeAttributeArgument>().FirstOrDefault(a => a.Name == argumentName); if (arg == null) { return(null); } return(VsxHelper.ParseCodeStringValue(arg.Value, arg.Language)); }
public void Attributes_ClassHasOneAttribute_ReturnsOneAttribute() { CreateCodeType("[TestAttribute] public class TestClass {}"); global::EnvDTE.CodeElements attributes = codeType.Attributes; CodeAttribute2 attribute = attributes.Item(1) as CodeAttribute2; Assert.AreEqual(1, attributes.Count); Assert.AreEqual("Test", attribute.Name); }
public static TResult GetProperty <T, TResult>(this CodeAttribute2 attr, Expression <Func <T, TResult> > propExpr) { var propName = ExprToString(propExpr); var codeAttributeArgument = attr.GetArguments().SingleOrDefault(a => a.Name == propName); string propValue = null; if (codeAttributeArgument != null) { propValue = codeAttributeArgument.Value; } return(ParseAttributeProperty <TResult>(propValue)); }
/// <summary> /// Convert CodeAttribute into an actual Attribute instance /// </summary> /// <typeparam name="TAttr"></typeparam> /// <param name="codeAttr"></param> /// <returns></returns> public static TAttr ToAttribute <TAttr>(this CodeAttribute2 codeAttr) where TAttr : Attribute, new() { var typeCache = TypeResolver.ByType <TAttr>(); var attr = new TAttr(); foreach (var arg in codeAttr.GetArguments()) { var propInfo = (PropertyInfo)typeCache[arg.Name]; propInfo.SetValue(attr, ParseAttributeProperty(propInfo.PropertyType, arg.Value)); } return(attr); }
/// <summary> /// Finds the or create custom attribute element. /// </summary> /// <param name="clazz">The clazz.</param> /// <param name="name">The name.</param> /// <param name="value">The value.</param> /// <returns></returns> public static CodeAttribute2 FindOrCreateCustomAttributeElement(CodeClass clazz, string name, string value) { foreach (CodeElement ce in clazz.Attributes) { CodeAttribute2 attr = ce as CodeAttribute2; if (attr != null && attr.Name == name) { return(attr); } } return((CodeAttribute2)clazz.AddAttribute(name, value, -1)); }
public void Attributes_MethodHasOneAttribute_ReturnsOneAttribute() { CreatePublicFunction("MyClass.MyFunction"); AddMethodAttribute("System.ObsoleteAttribute"); CodeElements attributes = codeFunction.Attributes; CodeAttribute2 attribute = attributes.FirstCodeAttribute2OrDefault(); Assert.AreEqual(1, attributes.Count); Assert.AreEqual("System.ObsoleteAttribute", attribute.FullName); }
public void Attributes_ParameterHasOneAttribute_ReturnsOneAttribute() { CreateParameter(); helper.AddAttributeToParameter("System.Web.Mvc.BindAttribute"); CodeElements attributes = parameter.Attributes; CodeAttribute2 attribute = parameter.Attributes.FirstCodeAttribute2OrDefault(); Assert.AreEqual(1, attributes.Count); Assert.AreEqual("System.Web.Mvc.BindAttribute", attribute.FullName); }
public void Attributes_PropertyHasOneAttribute_ReturnsOneAttribute() { helper.CreateProperty("MyProperty"); helper.AddAttribute("Tests.TestAttribute", "TestAttribute"); CreateCodeProperty2(); CodeElements attributes = property.Attributes; CodeAttribute2 attribute = attributes.Item(1) as CodeAttribute2; Assert.AreEqual(1, attributes.Count); Assert.AreEqual("Tests.TestAttribute", attribute.FullName); }
private bool PageMethodNeedsRepair(CodeFunction2 method) { if (method == null || method.Reference == null) { return(false); } ParameterInfo[] parameters = _signature.GetParameters(); // Tweak the signature so it's appropriate if (!method.IsShared) { return(true); } if (method.Access != vsCMAccess.vsCMAccessPublic) { return(true); } int i = 0; foreach (object p in method.Parameters) { CodeParameter2 parameter = new CodeParameter2(p); if ((parameter.Reference == null) || (string.Compare(parameter.Name, parameters[i++].Name, StringComparison.Ordinal) != 0)) { return(true); } } // Add the necessary attributes bool hasWebMethod = false; bool hasScriptMethod = false; foreach (object attr in method.Attributes) { CodeAttribute2 attribute = new CodeAttribute2(attr); if (attribute.Reference == null) { continue; } hasWebMethod |= !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Contains("WebMethod"); hasScriptMethod |= !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Contains("ScriptMethod"); if (hasWebMethod && hasScriptMethod) { break; } } return(!hasWebMethod || !hasScriptMethod); }
private void RepairPageMethod(CodeFunction2 method) { if (method == null || method.Reference == null) { return; } // Tweak the signature so it's appropriate method.IsShared = true; method.Access = vsCMAccess.vsCMAccessPublic; int i = 0; ParameterInfo[] parameters = _signature.GetParameters(); foreach (object p in method.Parameters) { CodeParameter2 parameter = new CodeParameter2(p); if (parameter.Reference != null) { parameter.Name = parameters[i++].Name; } } // Add the necessary attributes bool hasWebMethod = false; bool hasScriptMethod = false; foreach (object attr in method.Attributes) { CodeAttribute2 attribute = new CodeAttribute2(attr); if (attribute.Reference == null) { continue; } hasWebMethod |= !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Contains("WebMethod"); hasScriptMethod |= !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Contains("ScriptMethod"); if (hasWebMethod && hasScriptMethod) { break; } } if (!hasWebMethod) { method.AddAttribute(typeof(WebMethodAttribute).FullName, "", -1); } if (!hasScriptMethod) { method.AddAttribute(typeof(ScriptMethodAttribute).FullName, "", -1); } }
public void Attributes_ClassHasOneAttribute_ReturnsOneAttribute() { CreateProjectContent(); CreateClass("TestClass"); AddAttributeToClass("TestAttribute"); CreateCodeType(); global::EnvDTE.CodeElements attributes = codeType.Attributes; CodeAttribute2 attribute = attributes.Item(1) as CodeAttribute2; Assert.AreEqual(1, attributes.Count); Assert.AreEqual("Test", attribute.Name); }
private StepBindingNew GetBingingFromAttribute(CodeAttribute2 codeAttribute, CodeFunction codeFunction, BindingType bindingType, BindingScopeNew bindingScope) { try { if (codeAttribute.FullName.Equals(string.Format("TechTalk.SpecFlow.{0}Attribute", bindingType))) { return(CreateStepBinding(codeAttribute, codeFunction, bindingType, bindingScope)); } return(null); } catch (Exception) { return(null); } }
public void Attributes_ParameterHasOneAttribute_ReturnsOneAttribute() { CreateParameter( "using System;\r\n" + "public class MyClass {\r\n" + " public void MyMethod([Obsolete] int parameter) {}\r\n" + "}"); global::EnvDTE.CodeElements attributes = parameter.Attributes; CodeAttribute2 attribute = parameter.Attributes.FirstCodeAttribute2OrDefault(); Assert.AreEqual(1, attributes.Count); Assert.AreEqual("System.ObsoleteAttribute", attribute.FullName); }
public void Attributes_PropertyHasOneAttribute_ReturnsOneAttribute() { CreateCodeProperty2( "class MyClass {\r\n" + " [System.Obsolete]\r\n" + " public int MyProperty { get; set; }\r\n" + "}"); global::EnvDTE.CodeElements attributes = property.Attributes; CodeAttribute2 attribute = attributes.Item(1) as CodeAttribute2; Assert.AreEqual(1, attributes.Count); Assert.AreEqual("System.ObsoleteAttribute", attribute.FullName); }
private BindingScopeNew GetBingingScopeFromAttribute(CodeAttribute2 codeAttribute) { try { if (codeAttribute.FullName.Equals(typeof(StepScopeAttribute).FullName)) { var tag = GetStringArgumentValue(codeAttribute, "Tag"); string feature = GetStringArgumentValue(codeAttribute, "Feature"); string scenario = GetStringArgumentValue(codeAttribute, "Scenario"); if (tag == null && feature == null && scenario == null) return null; return new BindingScopeNew(tag, feature, scenario); } return null; } catch (Exception) { return null; } }
private BindingScope GetBingingScopeFromAttribute(CodeAttribute2 codeAttribute) { try { if (IsScopeAttribute(codeAttribute)) { var tag = GetStringArgumentValue(codeAttribute, "Tag"); string feature = GetStringArgumentValue(codeAttribute, "Feature"); string scenario = GetStringArgumentValue(codeAttribute, "Scenario"); if (tag == null && feature == null && scenario == null) return null; return new BindingScope(tag, feature, scenario); } return null; } catch (Exception) { return null; } }
private StepDefinitionBinding CreateStepBinding(CodeAttribute2 attr, CodeFunction codeFunction, StepDefinitionType stepDefinitionType, BindingScope bindingScope) { try { IBindingMethod bindingMethod = bindingReflectionFactory.CreateBindingMethod(codeFunction); var regexArg = attr.Arguments.Cast<CodeAttributeArgument>().FirstOrDefault(); if (regexArg == null) return null; var regexString = VsxHelper.ParseCodeStringValue(regexArg.Value, regexArg.Language); return new StepDefinitionBinding(stepDefinitionType, regexString, bindingMethod, bindingScope); } catch(Exception) { return null; } }
private StepBindingNew CreateStepBinding(CodeAttribute2 attr, CodeFunction codeFunction, BindingType bindingType, BindingScopeNew bindingScope) { try { IBindingMethod bindingMethod = new VsBindingMethod(codeFunction); var regexArg = attr.Arguments.Cast<CodeAttributeArgument>().FirstOrDefault(); if (regexArg == null) return null; var regexString = VsxHelper.ParseCodeStringValue(regexArg.Value, regexArg.Language); var regex = new Regex("^" + regexString + "$", RegexOptions.Compiled | RegexOptions.CultureInvariant); return new StepBindingNew(bindingMethod, bindingType, regex, bindingScope); } catch(Exception) { return null; } }
private StepBindingNew GetBingingFromAttribute(CodeAttribute2 codeAttribute, CodeFunction codeFunction, BindingType bindingType, BindingScopeNew bindingScope) { try { if (codeAttribute.FullName.Equals(string.Format("TechTalk.SpecFlow.{0}Attribute", bindingType))) return CreateStepBinding(codeAttribute, codeFunction, bindingType, bindingScope); return null; } catch(Exception) { return null; } }
private CodeDomAttributeMetadata(CodeAttribute2 codeAttribute, CodeDomFileMetadata file) { this.codeAttribute = codeAttribute; this.file = file; }
private bool IsGeneralStepDefinition(CodeAttribute2 codeAttribute) { try { return codeAttribute.FullName.Equals(typeof (StepDefinitionAttribute).FullName); } catch(Exception) { return false; } }
private void ParseAttribute(CodeAttribute2 codeAttribute, AccessControlledElement element, string currentFile) { WriteLine("ParseAttribute" + " " + codeAttribute.Name); var attribute = new Attribute { Name = codeAttribute.Name, FileName = currentFile}; foreach (CodeAttributeArgument codeArgument in codeAttribute.Arguments) { var value = codeArgument.Value; if (value.StartsWith("\"") && value.EndsWith("\"")) { value = value.Substring(1, value.Length - 2); } var argument = new Argument {Name = codeArgument.Name, Value = value}; attribute.Arguments.Add(argument); } element.Attributes.Add(attribute); }
private IEnumerable<StepBindingNew> GetStepDefinitionsFromAttribute(CodeAttribute2 codeAttribute, CodeFunction codeFunction, BindingScopeNew bindingScope) { var normalStepDefinition = GetBingingFromAttribute(codeAttribute, codeFunction, BindingType.Given, bindingScope) ?? GetBingingFromAttribute(codeAttribute, codeFunction, BindingType.When, bindingScope) ?? GetBingingFromAttribute(codeAttribute, codeFunction, BindingType.Then, bindingScope); if (normalStepDefinition != null) { yield return normalStepDefinition; yield break; } if (IsGeneralStepDefinition(codeAttribute)) { yield return CreateStepBinding(codeAttribute, codeFunction, BindingType.Given, bindingScope); yield return CreateStepBinding(codeAttribute, codeFunction, BindingType.When, bindingScope); yield return CreateStepBinding(codeAttribute, codeFunction, BindingType.Then, bindingScope); } }
internal ShellCodeAttribute(CodeAttribute2 codeAttribute2) : base(codeAttribute2 as CodeElement2) { _attribute = codeAttribute2; }
public CodeAttributeNodeFactory(CodeAttribute2 element) : base(element as CodeElement) { _attribute = element; }
private static bool CanProcessTypeAttribute(IdeBindingSourceProcessor bindingSourceProcessor, CodeAttribute2 attr) { string attributeTypeName; try { attributeTypeName = attr.FullName; } catch (Exception) { // invalid attribute - ignore return false; } return bindingSourceProcessor.CanProcessTypeAttribute(attributeTypeName); }
private bool IsScopeAttribute(CodeAttribute2 codeAttribute) { return codeAttribute.FullName.Equals(typeof(ScopeAttribute).FullName) || #pragma warning disable 612,618 codeAttribute.FullName.Equals(typeof(StepScopeAttribute).FullName); #pragma warning restore 612,618 }
/// <summary> /// /// </summary> public virtual AttributeInfo CreateAttribute(CodeAttribute2 item) { return new AttributeInfo(item); }