Click here to Skip to main content
15,894,646 members
Articles / Desktop Programming / XAML

Silverlight 1.0 Full JavaScript Intellisense

Rate me:
Please Sign up or sign in to vote.
3.81/5 (16 votes)
21 Dec 2007Ms-PL11 min read 31.9K   141   17  
An article about Silverlight 1.0 full JavaScript Intellisense
using System;
using System.Collections.Generic;
using System.Reflection;

namespace SiliverlightJavascriptIntelli
{
    public class SilverlightMethod : IJavascriptConvertable
    {
        private string name;
        private string returnValueType;
        private List<SilverlightParameter> parameters = new List<SilverlightParameter>();
        private bool isReturnValueTypeInteger;
        private SilverlightType belongTo;
        private string description;


        public static SilverlightMethod ConvertMethodInfoToSilverlightMethod(MethodInfo curMethodInfoToConvert, SilverlightType typeToAddTo)
        {
            SilverlightMethod newMethod = new SilverlightMethod(typeToAddTo);

            newMethod.Name = curMethodInfoToConvert.Name;
            newMethod.ReturnValueType = TypeHelper.ConvertTypeToString(curMethodInfoToConvert.ReturnType);
            newMethod.IsReturnValueTypeInteger = TypeHelper.IsTypeInteger(curMethodInfoToConvert.ReturnType);
            newMethod.Description = ReferenceAnalyzer.GetDescrptionForMethod(newMethod.Name);

            foreach (ParameterInfo curParameterInfoToConvertToSilverlightParam in curMethodInfoToConvert.GetParameters())
            {
                newMethod.Parameters.Add(
                    SilverlightParameter.ConvertParameterInfoToSilverlightParameter(curParameterInfoToConvertToSilverlightParam, newMethod));
            }

            return newMethod;
        }

        public string Name
        {
            get
            {
                return StringHelper.FirstLetterToLower(name);
            }
            set
            {
                name = value;
            }
        }

        public string ReturnValueType
        {
            get
            {
                return returnValueType;
            }
            set
            {
                returnValueType = value;
            }
        }

        public List<SilverlightParameter> Parameters
        {
            get
            {
                return parameters;
            }
        }

        public bool IsReturnValueTypeInteger
        {
            get
            {
                return isReturnValueTypeInteger;
            }
            set
            {
                isReturnValueTypeInteger = value;
            }
        }

        public SilverlightType BelongTo
        {
            get
            {
                return belongTo;
            }
            set
            {
                belongTo = value;
            }
        }

        private string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }

        public SilverlightMethod(SilverlightType belongTo)
        {
            this.belongTo = belongTo;
        }

        public string ToJavascript()
        {

            string ReturnValue = string.Empty;

            ReturnValue += StringHelper.Format(JavascriptFormats.FunctionHeader, this.Name,
                GetAllParametersByNameForSignature(), this.Description);

            ReturnValue += GetAllParametersComments();

            if (this.ReturnValueType != null)
                ReturnValue += GetJavascriptReturnsCommentForType(this.ReturnValueType,
                                                                  this.IsReturnValueTypeInteger.ToString());

            ReturnValue += StringHelper.Format(JavascriptFormats.JavacriptFunctionInvocationFormat,
                                               this.Name,
                                               GetAllParametersByNameForSignature(),
                                                TypeHelper.GetConvertToPrefixForType(this.ReturnValueType),
                                                 GetAllParametersByNameForInvocation(),
                                                GetAllParametersWithNameAndTypeForTypeSafety())
                                                + JavascriptFormats.OneBreakLine;


            ReturnValue += JavascriptFormats.FunctionEnd;

            return ReturnValue;
        }

        private string GetAllParametersWithNameAndTypeForTypeSafety()
        {
            List<string> paramNames =
                 this.Parameters.ConvertAll<string>(
                     delegate(SilverlightParameter param)
                     {
                         return  "\"" + param.Type + "\":" + param.Name;
                     });

            return "{" +  String.Join(", ", paramNames.ToArray()) + "}";
        }

        private string GetAllParametersByNameForInvocation()
        {
            List<string> paramNames =
                 this.Parameters.ConvertAll<string>(
                     delegate(SilverlightParameter param)
                     {
                         return param.Name + TypeHelper.GetExtractFromPrefixFor(param.Type);
                     });

            return String.Join(", ", paramNames.ToArray());
        }


        public static string GetJavascriptReturnsCommentForType(string type, string IsInteger)
        {
            return StringHelper.Format(JavascriptFormats.JavascriptReturnsCommentFormat, type, IsInteger);
        }

        public string GetAllParametersComments()
        {
            string ReturnValue = string.Empty;

            foreach (SilverlightParameter parameterToAddToJavascriptComments in this.Parameters)
            {
                ReturnValue += parameterToAddToJavascriptComments.ToJavascript();
            }

            return ReturnValue;
        }

        public string GetAllParametersByNameForSignature()
        {
            List<string> paramNames =
                this.Parameters.ConvertAll<string>(
                    delegate(SilverlightParameter param)
                    {
                        return param.Name;
                    });

            return String.Join(", ", paramNames.ToArray());
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
JustinAngel.Net, Senior .Net consultant
Israel Israel
Justin-Josef Angel is a C# Microsoft Most Valuable professional, a Senior .Net consultant in Israel with 4 years of .Net experience and has 8 years of Web experience.

Justin's been working this past year on two Enterprise sized Silverlight projects with his customers. During that time he's gained a real-insight into Silverlight's inner workings and how to integrate Silverlight into the real world of software development. Additionally, During that time he's developed a few well-known projects like the "Silverlight 1.0 Javascript Intellisense", "Silverlight 1.1 Hebrew & Arabic Languages support" and a few others you might know.

Justin is also a seasoned presenter with an impressive track-record of talking in front of thousands of people in Israel.

Justin owns the first .Net blog written in Hebrew - http://www.JustinAngel.Net .
And he also owns an additional blog with mixed Hebrew & English content - http://blogs.Microsoft.co.il/blogs/JustinAngel.

A full list of his articles (all 100+ of them) can be found at: http://www.JustinAngel.Net/#index




Comments and Discussions