111,131
社区成员
发帖
与我相关
我的任务
分享
using System;
namespace ReflectionEP
{
public abstract class ClassMain
{
public abstract void WriteString(string str);
}
}
using System;
namespace ReflectionEP
{
[Serializable]
public class Class2:ClassMain
{
public override void WriteString(string str)
{
Console.WriteLine("this is childclass1:" + str);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ReflectionEP
{
class Program
{
static void Main(string[] args)
{
Assembly a = Assembly.LoadFrom("Class2.dll");
ClassMain c = (Class2)a.CreateInstance("ReflectionEP.Class2");//这里出问题了,提示无法将类型为“ReflectionEP.Class2”的对象强制转换为类型“ReflectionEP.Class2”。
string str1 = Console.ReadLine();
c.WriteString(str1);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsApplication16
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Assembly a = Assembly.GetExecutingAssembly(); // 测试方便简单写法
Object Obj = a.CreateInstance("WindowsApplication16.Class2");
((IClassMain)Obj).WriteString("aaa");
}
}
public interface IClassMain
{
void WriteString(string str);
}
public class Class2 : IClassMain
{
public void WriteString(string str)
{
MessageBox.Show("this is childclass1:" + str);
}
}
}