private static async Task <Document> Refactor(Document document, SyntaxNode nodeToFix, LayoutKind layoutKind, CancellationToken cancellationToken) { var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); var generator = editor.Generator; var semanticModel = editor.SemanticModel; var structLayoutAttribute = semanticModel.Compilation.GetTypeByMetadataName("System.Runtime.InteropServices.StructLayoutAttribute"); var layoutKindEnum = semanticModel.Compilation.GetTypeByMetadataName("System.Runtime.InteropServices.LayoutKind"); var attribute = editor.Generator.Attribute( generator.TypeExpression(structLayoutAttribute, addImport: true), new[] { generator.AttributeArgument( generator.MemberAccessExpression( generator.TypeExpression(layoutKindEnum, addImport: true), layoutKind.ToString())), }); var structNode = (StructDeclarationSyntax)nodeToFix; editor.AddAttribute(structNode, attribute); return(editor.GetChangedDocument()); }
public override string ToText() { var builder = new StringBuilder(); builder.Append("StructLayout(LayoutKind."); builder.Append(LayoutKind.ToString()); if (CharSet.HasValue) { builder.Append(", CharSet = CharSet."); builder.Append(CharSet.Value.ToString()); } if (Pack.HasValue) { builder.Append(", Pack = "); builder.Append(Pack.Value.ToString()); } if (Size.HasValue) { builder.Append(", Size = "); builder.Append(Size.Value.ToString()); } builder.Append(")"); return(builder.ToString()); }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1: Return the Value property in StructLayoutAttribute class 1"); try { LayoutKind mylayoutkind = LayoutKind.Auto; StructLayoutAttribute myInstance = new StructLayoutAttribute(mylayoutkind); if (myInstance.Value != mylayoutkind) { TestLibrary.TestFramework.LogError("001", "the ExpectResult is " + mylayoutkind.ToString() + " but the ActualResult is " + myInstance.Value.ToString()); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002", "Unexpect exception:" + e); retVal = false; } return(retVal); }
/// <summary> /// Generate the StructLayoutAttribute attribute /// </summary> /// <param name="kind"></param> /// <returns></returns> /// <remarks></remarks> static internal CodeAttributeDeclaration CreateStructLayoutAttribute(LayoutKind kind) { CodeTypeReference attrRef = new CodeTypeReference(typeof(System.Runtime.InteropServices.StructLayoutAttribute)); CodeAttributeDeclaration attr = new CodeAttributeDeclaration(new CodeTypeReference(typeof(StructLayoutAttribute))); attr.Arguments.Add(new CodeAttributeArgument(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(LayoutKind)), kind.ToString()))); return(attr); }