示例#1
0
        public override object VisitDeclareDeclaration(DeclareDeclaration declareDeclaration, object data)
        {
            if (usings != null && !usings.ContainsKey("System.Runtime.InteropServices"))
            {
                UsingDeclaration @using = new UsingDeclaration("System.Runtime.InteropServices");
                addedUsings.Add(@using);
                base.VisitUsingDeclaration(@using, data);
            }

            MethodDeclaration method = new MethodDeclaration {
                Name          = declareDeclaration.Name,
                Modifier      = declareDeclaration.Modifier,
                TypeReference = declareDeclaration.TypeReference,
                Parameters    = declareDeclaration.Parameters,
                Attributes    = declareDeclaration.Attributes
            };

            if ((method.Modifier & Modifiers.Visibility) == 0)
            {
                method.Modifier |= Modifiers.Public;
            }
            method.Modifier |= Modifiers.Extern | Modifiers.Static;

            if (method.TypeReference.IsNull)
            {
                method.TypeReference = new TypeReference("System.Void", true);
            }

            Attribute att = new Attribute("DllImport", null, null);

            att.PositionalArguments.Add(CreateStringLiteral(declareDeclaration.Library));
            if (declareDeclaration.Alias.Length > 0)
            {
                att.NamedArguments.Add(new NamedArgumentExpression("EntryPoint", CreateStringLiteral(declareDeclaration.Alias)));
            }
            switch (declareDeclaration.Charset)
            {
            case CharsetModifier.Auto:
                att.NamedArguments.Add(new NamedArgumentExpression("CharSet",
                                                                   new MemberReferenceExpression(new IdentifierExpression("CharSet"),
                                                                                                 "Auto")));
                break;

            case CharsetModifier.Unicode:
                att.NamedArguments.Add(new NamedArgumentExpression("CharSet",
                                                                   new MemberReferenceExpression(new IdentifierExpression("CharSet"),
                                                                                                 "Unicode")));
                break;

            default:
                att.NamedArguments.Add(new NamedArgumentExpression("CharSet",
                                                                   new MemberReferenceExpression(new IdentifierExpression("CharSet"),
                                                                                                 "Ansi")));
                break;
            }
            att.NamedArguments.Add(new NamedArgumentExpression("SetLastError", new PrimitiveExpression(true, true.ToString())));
            att.NamedArguments.Add(new NamedArgumentExpression("ExactSpelling", new PrimitiveExpression(true, true.ToString())));
            method.Attributes.Add(new AttributeSection {
                Attributes = { att }
            });
            ReplaceCurrentNode(method);
            return(base.VisitMethodDeclaration(method, data));
        }
		public override object VisitDeclareDeclaration(DeclareDeclaration declareDeclaration, object data)
		{
			if (usings != null && !usings.ContainsKey("System.Runtime.InteropServices")) {
				UsingDeclaration @using = new UsingDeclaration("System.Runtime.InteropServices");
				addedUsings.Add(@using);
				base.VisitUsingDeclaration(@using, data);
			}
			
			MethodDeclaration method = new MethodDeclaration {
				Name = declareDeclaration.Name,
				Modifier = declareDeclaration.Modifier,
				TypeReference = declareDeclaration.TypeReference,
				Parameters = declareDeclaration.Parameters,
				Attributes = declareDeclaration.Attributes
			};
			
			if ((method.Modifier & Modifiers.Visibility) == 0)
				method.Modifier |= Modifiers.Public;
			method.Modifier |= Modifiers.Extern | Modifiers.Static;
			
			if (method.TypeReference.IsNull) {
				method.TypeReference = new TypeReference("System.Void", true);
			}
			
			Attribute att = new Attribute("DllImport", null, null);
			att.PositionalArguments.Add(CreateStringLiteral(declareDeclaration.Library));
			if (declareDeclaration.Alias.Length > 0) {
				att.NamedArguments.Add(new NamedArgumentExpression("EntryPoint", CreateStringLiteral(declareDeclaration.Alias)));
			}
			switch (declareDeclaration.Charset) {
				case CharsetModifier.Auto:
					att.NamedArguments.Add(new NamedArgumentExpression("CharSet",
					                                                   new MemberReferenceExpression(new IdentifierExpression("CharSet"),
					                                                                                 "Auto")));
					break;
				case CharsetModifier.Unicode:
					att.NamedArguments.Add(new NamedArgumentExpression("CharSet",
					                                                   new MemberReferenceExpression(new IdentifierExpression("CharSet"),
					                                                                                 "Unicode")));
					break;
				default:
					att.NamedArguments.Add(new NamedArgumentExpression("CharSet",
					                                                   new MemberReferenceExpression(new IdentifierExpression("CharSet"),
					                                                                                 "Ansi")));
					break;
			}
			att.NamedArguments.Add(new NamedArgumentExpression("SetLastError", new PrimitiveExpression(true, true.ToString())));
			att.NamedArguments.Add(new NamedArgumentExpression("ExactSpelling", new PrimitiveExpression(true, true.ToString())));
			method.Attributes.Add(new AttributeSection { Attributes = { att } });
			ReplaceCurrentNode(method);
			return base.VisitMethodDeclaration(method, data);
		}