如何使用Windoes电源shell执行C#功能 [英] How to excute C# function using Windoes power shell

查看:125
本文介绍了如何使用Windoes电源shell执行C#功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中使用。使用它我可以执行C#代码。但我想只执行C#功能。请任何人都可以帮助..........



I am use below code. using that i can execute C# code. but I want to execute only C# function. pls any one can help ..........

$source = @"
public class BasicTestererr
{
    public static int Add(int a, int b)
    {
        return (a - b);
    }

    public int Multiply(int a, int b)
    {
        return (a * b);
    }
}
"@

Add-Type -TypeDefinition $source
Add(4, 1)

推荐答案

source = @
public class BasicTestererr
{
public static int Add(int a,int b)
{
return(a - b);
}

public int Multiply(int a,int b)
{
return(a * b);
}
}
@

Add-Type -TypeDefinition
source = @" public class BasicTestererr { public static int Add(int a, int b) { return (a - b); } public int Multiply(int a, int b) { return (a * b); } } "@ Add-Type -TypeDefinition


source
Add(4,1)
source Add(4, 1)


奇怪的想法。我不认为你的整个想法真的很有用,但是......



您可以创建一个程序集主机.NET应用程序,它使用函数加载程序集 Add Multiply 或任何其他使用Reflection的方法,查找要通过其全名调用的方法(例如BasicTestererr。添加),它的形式参数和返回类型,使用一些实际参数调用此方法,返回值或处理错误,在这种情况下很可能。此工具还可以显示反射生成的文档,向用户显示预期的参数以及如何解释返回的值。



现在,关于PowerShell或任何其他批处理语言。您可以使此主机应用程序接受命令行参数,包括要加载的程序集(可以是程序集的主可执行模块的文件名),要调用的方法和实际方法参数。这种方法应限制在参数类型和返回类型中:它们可以是字符串类型或可从/到字符串转换的类型,因为命令行界面仅适用于字符串。您还需要开发一种报告错误的方法,在未找到文件,未找到入口点,参数数量错误或参数值违反约束的情况下;因为它们被表示为字符串,所以很容易出错。



-SA
Strange idea. I don''t think your whole idea is really useful, but…

You can create an "assembly host" .NET application which loads your assembly with functions Add, Multiply or any other, using Reflection, finds the method to be called by its full name (for example "BasicTestererr.Add"), its formal parameters and return type, call this method with some actual parameters, return the value or process the error, which would be very likely in this case. This tool could also show Reflection-generated documentation showing to the user what parameters are expected and how to interpret the returned value.

Now, about the PowerShell or any other batch language. You can make this "host" application accepting command-line parameters including assembly to be loaded (could be a name of the file of the main executable module of the assembly), a method to be called and actual method parameters. This approach should be limited in the types of parameters and return types: they can be string types or types convertable from/to string, as command line interface only works with strings. You also need to develop a method of reporting errors, in situation when a file not found, entry point is not found, wrong number of parameter or violation of constraints for parameters values; as they are represented as strings, it''s easy to make a mistake.

—SA


这篇关于如何使用Windoes电源shell执行C#功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆