原来要从同一个接口继承才能用Activator.CreateInstance来CAST

下午在3203听张齐勋和郭文海老师的嵌入式软件开发与工具,因为各位同学都在进行开题报告,我就在最后一排拼命CAST
怎么也cast不过去,总是Unable to cast …. to …..
开始我用的接口文件是两个文件。就是原来的库从一个接口继承,然后新的cast强制转换为一个相同结构不同文件名的另一个接口!

IClass1 _instance = (IClass1)Activator.CreateInstance(System.Type.GetType("ClassLibrary1.Class1,ClassLibrary1"));

开始我这两个接口仅仅是命名空间不一样,无法cast
我想应该和命名空间没关系啊,然后我把命名空间改成一样的,还不行。
哦,我突然明白应该从同一个文件cast。
不过我还是不理解为什么完全一样的接口文件仅仅换了个地方就不能cast呢??
于是我把正确的cast的方法写在下面,省得自己以后走冤枉路。
———————————————————————————————-
首先建立一个公用的接口

using System;

namespace ClassLibrary2
{
    public interface IClass1
    {
        string printName();
    }
}

然后从这个接口建类


using System;
using System.Collections.Generic;
using System.Text;

namespace ClassLibrary1
{
    public class Class1 : ClassLibrary2.IClass1
    {
        public Class1()
        {
        }
        public string printName()
        {
            return "s";
        }

    }
}


最后再cast的时候还从原来那个文件接口cast



            ClassLibrary2.IClass1 _instance = (ClassLibrary2.IClass1)Activator.CreateInstance(System.Type.GetType("ClassLibrary1.Class1,ClassLibrary1"));
            string s=_instance.printName();
            Response.Write(s);

哦也
cast过来了
————
当当,下面打算研究
Activator.CreateInstance

Assembly.LoadFrom
的效率问题
————
当当,最近看了一部动画片叫做“斗牌传说”,不错,画风开始不喜欢,后来觉得,恩,也算可以了。
一部消遣的动画。还算可以吧。深度不够。
————
当当,最近看完了一部动画“蓝蓝岛漂流记”
这个很有意思
是我所认为的最好的画风之一
虽然情节很一般
————
当当,结束。怎么挣钱呢?

7 comments

  1. lovehr 说道:

    当当,有点晕Sad

  2. Yee Feuerberg 说道:

    Excellent beat ! I would like to apprentice at the same time as you amend your site, how could i subscribe for a weblog site? The account aided me a applicable deal. I were a little bit familiar of this your broadcast offered vibrant clear idea

  3. Adolph Scicchitano 说道:

    I have seen that charges for on-line degree gurus tend to be a great value. For example a full Bachelor’s Degree in Communication with the University of Phoenix Online consists of Sixty credits at $515/credit or $30,900. Also American Intercontinental University Online offers a Bachelors of Business Administration with a complete education course element of 180 units and a tariff of $30,560. Online studying has made having your college degree been so cool because you might earn your current degree from the comfort of your abode and when you finish from office. Thanks for other tips I have certainly learned from your web site.

  4. Dori Paszek 说道:

    Great post. I’m a normal visitor of your blog and appreciate you taking the time to maintain the nice site. I will be a frequent visitor for a really long time.

  5. Kimberly Lelacheur 说道:

    One extra method for promoting your webpage is posting comments on unique directories with your web site link.

发表评论

电子邮件地址不会被公开。