反射_程序集_额外补充2(将"123"转"壹贰叁"的stringbuilder打包成程序集类库的方法)
程序员文章站
2022-05-04 11:42:00
...
反射_程序集_额外补充2<18/9/2017>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace 反射
{
class Program
{
static void Main(string[] args)
{
//程序集
Assembly aa = Assembly.LoadFile(@"C:\Users\Maximilian Liu\Desktop\ClassLibrary1.dll");//使用LoadFile而不是Load
Type[] tp = aa.GetTypes();
Console.WriteLine(tp.Length);
object o = tp[0].GetConstructors()[0].Invoke(null);
MethodInfo m = tp[0].GetMethod("M");
m.Invoke(o, new object[] { "123214" });
}
}
}
ClassLibrary1.dll内容为下:(stringbuilder相关)using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLibrary1
{
public class Class1
{
public void M(string str)//123 -->> 一二三
{
StringBuilder MyStringBuilder = new StringBuilder(str);
//第二种用Append实现StringBuilder MyStringBuilder = new StringBuilder()
string s = "零壹贰叁肆伍陆柒捌玖";
for (int i = 0; i < str.Length; i++)
{
int a = str[i] - 48;
MyStringBuilder.Replace(str[i], s[a]);
}
//for (int i = 0; i < str.Length; i++)
//{
// int a = str[i] - 48;
// MyStringBuilder.Append(s[a]);
//}
Console.WriteLine(MyStringBuilder);
}
}
}
需要将该程序集版本改为framwork3.5,不然拿到unity中无法直接引用出命名空间
请参考作者另外一个和程序集相关的博客