本文整理汇总了C#中System.CodeDom.CodeTypeDeclaration.AddExtensionMethod方法的典型用法代码示例。如果您正苦于以下问题:C# CodeTypeDeclaration.AddExtensionMethod方法的具体用法?C# CodeTypeDeclaration.AddExtensionMethod怎么用?C# CodeTypeDeclaration.AddExtensionMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.CodeDom.CodeTypeDeclaration
的用法示例。
在下文中一共展示了CodeTypeDeclaration.AddExtensionMethod方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DeclareExtensionMethod
/// <summary>
/// Declare the extension method given the function container class.
/// </summary>
/// <param name="functionExtensionClass">Function container class</param>
/// <param name="func">Function to add</param>
protected virtual void DeclareExtensionMethod(CodeTypeDeclaration functionExtensionClass, Function func)
{
// retrieve parameters and add the extension method
ServiceOperationAnnotation actionAnnotation = func.Annotations.OfType<ServiceOperationAnnotation>().Single();
ExceptionUtilities.Assert(actionAnnotation.BindingKind.IsBound(), "Action function cannot generate extension method with out function having a binding");
FunctionParameter bindingTypeParameter = func.Parameters[0];
CodeMemberMethod method = functionExtensionClass.AddExtensionMethod(
func.Name,
new CodeParameterDeclarationExpression(
this.GetParameterTypeOrFunctionReturnTypeReference(bindingTypeParameter.Annotations, bindingTypeParameter.DataType),
bindingTypeParameter.Name));
// add non-binding type parameters
foreach (var parameter in func.Parameters.Where(p => p.Name != bindingTypeParameter.Name))
{
method.Parameters.Add(
new CodeParameterDeclarationExpression(
this.GetParameterTypeOrFunctionReturnTypeReference(parameter.Annotations, parameter.DataType),
parameter.Name));
}
// throw exception if the client code method is called
method.Statements.Add(
new CodeThrowExceptionStatement(
new CodeObjectCreateExpression(
new CodeTypeReference(typeof(InvalidOperationException)),
new CodeExpression[] { })));
// add method return type
if (func.ReturnType != null)
{
method.ReturnType = this.GetParameterTypeOrFunctionReturnTypeReference(func.Annotations, func.ReturnType);
}
}