﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>博客园-phytan-最新评论</title><link>http://www.cnblogs.com/phytan/CommentsRSS.aspx</link><description>Useful Misc</description><language>zh-cn</language><pubDate>Wed, 11 Jul 2007 10:36:11 GMT</pubDate><lastBuildDate>Wed, 11 Jul 2007 10:36:11 GMT</lastBuildDate><generator>cnblogs</generator><item><title>Re:C#中实现VB中的CreateObject方法.</title><link>http://www.cnblogs.com/phytan/archive/2010/08/25/814175.html#1901591</link><dc:creator>失落映画</dc:creator><author>失落映画</author><pubDate>Wed, 25 Aug 2010 08:20:03 GMT</pubDate><guid>http://www.cnblogs.com/phytan/archive/2010/08/25/814175.html#1901591</guid><description><![CDATA[蛋疼... ...
用Reflector看下Microsfot.VisualBasic.Interaction.CreateObjectde的源碼就知道了... ...

弄那麼複雜高深做什麼呢?
其實就是用 
obj2 = Activator.CreateInstance(typeFromProgID);
實現的.

附帶源碼:

[code=csharp]
[SecurityPermission(SecurityAction.Demand, UnmanagedCode=true)]
public static object CreateObject(string ProgId, [Optional, DefaultParameterValue(&quot;&quot;)] string ServerName)
{
    object obj2;
    if (ProgId.Length == 0)
    {
        throw ExceptionUtils.VbMakeException(0x1ad);
    }
    if ((ServerName == null) || (ServerName.Length == 0))
    {
        ServerName = null;
    }
    else if (Thread.CurrentThread.CurrentCulture.CompareInfo.Compare(Environment.MachineName, ServerName, CompareOptions.IgnoreCase) == 0)
    {
        ServerName = null;
    }
    try
    {
        Type typeFromProgID;
        if (ServerName == null)
        {
            typeFromProgID = Type.GetTypeFromProgID(ProgId);
        }
        else
        {
            typeFromProgID = Type.GetTypeFromProgID(ProgId, ServerName, true);
        }
        obj2 = Activator.CreateInstance(typeFromProgID);
    }
    catch (COMException exception)
    {
        if (exception.ErrorCode == -2147023174)
        {
            throw ExceptionUtils.VbMakeException(0x1ce);
        }
        throw ExceptionUtils.VbMakeException(0x1ad);
    }
    catch (Exception)
    {
        throw ExceptionUtils.VbMakeException(0x1ad);
    }
    return obj2;
}

 

 

[/code]<br><br><div align=right><a style="text-decoration:none;" href="http://www.cnblogs.com/phytan/" target="_blank">失落映画</a> 2010-08-25 16:20 <a href="http://www.cnblogs.com/phytan/archive/2010/08/25/814175.html#1901591#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 在Vista中编程控制防火墙设定(C#)</title><link>http://www.cnblogs.com/phytan/archive/2009/01/20/814474.html#1435429</link><dc:creator>你大爷</dc:creator><author>你大爷</author><pubDate>Tue, 20 Jan 2009 05:44:03 GMT</pubDate><guid>http://www.cnblogs.com/phytan/archive/2009/01/20/814474.html#1435429</guid><description><![CDATA[莫谈国是！！！<br><br><div align=right><a style="text-decoration:none;" href="http://www.cnblogs.com/phytan/" target="_blank">你大爷</a> 2009-01-20 13:44 <a href="http://www.cnblogs.com/phytan/archive/2009/01/20/814474.html#1435429#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: C#中实现VB中的CreateObject方法.</title><link>http://www.cnblogs.com/phytan/archive/2008/10/29/814175.html#1354330</link><dc:creator>TH lin</dc:creator><author>TH lin</author><pubDate>Tue, 28 Oct 2008 20:28:22 GMT</pubDate><guid>http://www.cnblogs.com/phytan/archive/2008/10/29/814175.html#1354330</guid><description><![CDATA[VB的CreateObject:<br/><br/>Microsoft.VisualBasic.Interaction.CreateObject(strProgID, strServer);<br/><br/>不止可以輸入Com 的ProgID, 還可以輸入機器位址，想請教C＃中使用Activator 要如何指定COM元件所在的機器？<br><br><div align=right><a style="text-decoration:none;" href="http://www.cnblogs.com/phytan/" target="_blank">TH lin</a> 2008-10-29 04:28 <a href="http://www.cnblogs.com/phytan/archive/2008/10/29/814175.html#1354330#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: C#中实现VB中的CreateObject方法.</title><link>http://www.cnblogs.com/phytan/archive/2008/08/07/814175.html#1280688</link><dc:creator>leifa</dc:creator><author>leifa</author><pubDate>Thu, 07 Aug 2008 06:53:14 GMT</pubDate><guid>http://www.cnblogs.com/phytan/archive/2008/08/07/814175.html#1280688</guid><description><![CDATA[帮了大忙 ！！<br><br><div align=right><a style="text-decoration:none;" href="http://www.cnblogs.com/phytan/" target="_blank">leifa</a> 2008-08-07 14:53 <a href="http://www.cnblogs.com/phytan/archive/2008/08/07/814175.html#1280688#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 在Vista中编程控制防火墙设定(C#)</title><link>http://www.cnblogs.com/phytan/archive/2008/06/24/814474.html#1234178</link><dc:creator>咖啡猪</dc:creator><author>咖啡猪</author><pubDate>Tue, 24 Jun 2008 01:19:02 GMT</pubDate><guid>http://www.cnblogs.com/phytan/archive/2008/06/24/814474.html#1234178</guid><description><![CDATA[我添加了对NetFwTypeLib的引用，不过还是没那个INetFwPolicy2，只有INetFwPlicy，难道是操作系统的原因？<br>我的系统：Xp(Sp2) + Vs2005(sp1)<br><br><div align=right><a style="text-decoration:none;" href="http://www.cnblogs.com/phytan/" target="_blank">咖啡猪</a> 2008-06-24 09:19 <a href="http://www.cnblogs.com/phytan/archive/2008/06/24/814474.html#1234178#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 在Vista中编程控制防火墙设定(C#)</title><link>http://www.cnblogs.com/phytan/archive/2008/06/23/814474.html#1233776</link><dc:creator>phytan</dc:creator><author>phytan</author><pubDate>Mon, 23 Jun 2008 10:51:23 GMT</pubDate><guid>http://www.cnblogs.com/phytan/archive/2008/06/23/814474.html#1233776</guid><description><![CDATA[程序中添加引用,在COM对象中找到&quot;NetFwTypeLib&quot;<br><br><div align=right><a style="text-decoration:none;" href="http://www.cnblogs.com/phytan/" target="_blank">phytan</a> 2008-06-23 18:51 <a href="http://www.cnblogs.com/phytan/archive/2008/06/23/814474.html#1233776#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 在Vista中编程控制防火墙设定(C#)</title><link>http://www.cnblogs.com/phytan/archive/2008/06/23/814474.html#1233723</link><dc:creator>咖啡猪</dc:creator><author>咖啡猪</author><pubDate>Mon, 23 Jun 2008 09:38:29 GMT</pubDate><guid>http://www.cnblogs.com/phytan/archive/2008/06/23/814474.html#1233723</guid><description><![CDATA[INetFwPolicy2 这个是什么类型啊？怎么我在编译时提示“无法找到此类型”？<br><br><div align=right><a style="text-decoration:none;" href="http://www.cnblogs.com/phytan/" target="_blank">咖啡猪</a> 2008-06-23 17:38 <a href="http://www.cnblogs.com/phytan/archive/2008/06/23/814474.html#1233723#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: C#中实现VB中的CreateObject方法.</title><link>http://www.cnblogs.com/phytan/archive/2007/07/13/814175.html#817453</link><dc:creator>AndyHai</dc:creator><author>AndyHai</author><pubDate>Fri, 13 Jul 2007 09:06:00 GMT</pubDate><guid>http://www.cnblogs.com/phytan/archive/2007/07/13/814175.html#817453</guid><description><![CDATA[呵呵，这个好玩，有点像BCB里的 Variant::CreateObject<br><br><div align=right><a style="text-decoration:none;" href="http://www.cnblogs.com/phytan/" target="_blank">AndyHai</a> 2007-07-13 17:06 <a href="http://www.cnblogs.com/phytan/archive/2007/07/13/814175.html#817453#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: C#中实现VB中的CreateObject方法.</title><link>http://www.cnblogs.com/phytan/archive/2007/07/12/814175.html#816523</link><dc:creator>曲滨*銘龘鶽</dc:creator><author>曲滨*銘龘鶽</author><pubDate>Thu, 12 Jul 2007 15:23:00 GMT</pubDate><guid>http://www.cnblogs.com/phytan/archive/2007/07/12/814175.html#816523</guid><description><![CDATA[C# 的是反射<br>原理一样<br>只不过用着不是很方便！<br><br><div align=right><a style="text-decoration:none;" href="http://www.cnblogs.com/phytan/" target="_blank">曲滨*銘龘鶽</a> 2007-07-12 23:23 <a href="http://www.cnblogs.com/phytan/archive/2007/07/12/814175.html#816523#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: C#中实现VB中的CreateObject方法.</title><link>http://www.cnblogs.com/phytan/archive/2007/07/12/814175.html#815261</link><dc:creator>随风流月</dc:creator><author>随风流月</author><pubDate>Thu, 12 Jul 2007 03:36:00 GMT</pubDate><guid>http://www.cnblogs.com/phytan/archive/2007/07/12/814175.html#815261</guid><description><![CDATA[@JoeLee<br>但是也可以弱类型。<br><br><div align=right><a style="text-decoration:none;" href="http://www.cnblogs.com/phytan/" target="_blank">随风流月</a> 2007-07-12 11:36 <a href="http://www.cnblogs.com/phytan/archive/2007/07/12/814175.html#815261#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>
