public override CodeTypeMember CreateCastingOperator(
            string fromType, string toType, string argumentName, CodeStatementCollection statements,
            MemberAttributes attributes, CodeCastOperatorKind castOperatorKind)
        {
            StringBuilder sb = new StringBuilder();

            AppendMemberAttributeString(sb, c_memberAttributeKeywordMappings, attributes);
            if (castOperatorKind == CodeCastOperatorKind.Implicit)
            {
                sb.Append(" implicit operator ");
            }
            else
            {
                sb.Append(" explicit operator ");
            }

            sb.Append(toType);
            sb.Append(" (");
            sb.Append(fromType);
            sb.Append(" ");
            sb.Append(argumentName);
            sb.Append(") {");

            StringWriter writer = new StringWriter(sb);

            // CodeGeneratorOptions options  = new CodeGeneratorOptions ();

            foreach (CodeStatement statement in statements)
            {
                Generator.GenerateCodeFromStatement(statement, writer, null);
            }

            sb.Append("        }");

            return(new CodeSnippetTypeMember(sb.ToString()));
        }
예제 #2
0
 /// <summary>
 /// For derived classes that support casting operators, this method creates implicit or explicit casting operators.
 /// </summary>
 /// <param name="fromType">Type that the method casts from.</param>
 /// <param name="toType">Type that the method casts to.</param>
 /// <param name="argumentName">The name of the cast operator's argument.</param>
 /// <param name="statements">Statements that perform the conversion, ending with a <c>CodeMethodReturnStatement</c>.</param>
 /// <param name="attributes">Method attributes that define access and scope. Must be <see langword="static"/>.</param>
 /// <param name="castOperatorKind"><c>Implicit</c> to create an implicit casting operator, <c>Explicit</c> otherwise.</param>
 /// <returns>A <c>CodeTypeMember</c> object that can be appended to a CodeDOM type object.</returns>
 /// <exception cref="NotSupportedException">The default implementation always throws this exception.</exception>
 public virtual CodeTypeMember CreateCastingOperator(
     string fromType, string toType, string argumentName, CodeStatementCollection statements,
     MemberAttributes attributes, CodeCastOperatorKind castOperatorKind)
 {
     throw new NotSupportedException(this.GetType().FullName + " does not support casting operators.");
 }