Find the newly added methods and classes in Framework 4.0 (below)

Source: Internet
Author: User
Tags mscorlib

 

Problem description:Find the newly added methods and classes in Framework 4.0 (on)

 

Why cannot I find Framework 4.0 when I dynamically load the assembly?And Framwork2.0New methods and classes?

 

 

 

 

 

 

 

 

Because the console program adds the Framework4.0 assembly by default. When you use objects, types, and string classes, you are already using the loaded assembly,WhileClrThe Assembly will not be loaded repeatedly ??, This cannot be remembered.

So V2Assembly and v4Assembly are both Framework4.0 assemblies.

 

Verification:

 
static void Main(string[] args)
{
Assembly assemblyV2 = Assembly.LoadFile(
@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll");
Assembly assemblyV4 = Assembly.LoadFile(
@"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll");
 
Console. WriteLine ("V2 name {0} \ nV4 name {1}", assemblyV2.FullName, assemblyV4.FullName );
 
Console.ReadLine();
}
 

 

The output is as follows:

 

 

 

 

Because mscorlib. dll is an assembly in the Share Domain, so two different mscorlib cannot be loaded in the same application. dll. therefore, consider using two applications, one Framework 2.0 and the other Framework 4.0.

 

So you can change your mind:Use2.0 framework to create a program to call the WCF Service of framework4.0.

 

 

The code structure is as follows:

V4NewLooker is a Winform program based on framework 2.0.

V4WcfService is a framework-4.0-based WCF Service.

 

 

 

 

The interface is defined as follows:

 
namespace V4WcfService
{
// Note: You can use the "RENAME" command on the "refactoring" menu to change the Interface Name "IService1" in the Code and configuration file at the same time ".
[ServiceContract]
public interface IService1
{
[OperationContract]
List<TypeMembers> GetNewTypeMember(List<TypeMembers> lstOldTypes);
}
 
[DataContract]
public class TypeMembers
{
[DataMember]
public string FullName { get; set; }
 
[DataMember]
public List<string> MemberNames { get; set; }
}
}
 

 

The service implementation code is as follows:

 

 
namespace V4WcfService
{
// Note: You can use the "RENAME" command on the "refactoring" menu to change the class name "Service1" in the code, svc, and configuration file at the same time ".
public class Service1 : IService1
{
public List<TypeMembers> GetNewTypeMember(List<TypeMembers> lstOldTypes)
{
List<TypeMembers> result = new List<TypeMembers>();
 
Assembly mscorlibAssembly = typeof(object).Assembly;
Type[] v4Types = mscorlibAssembly.GetTypes();
 
# All types updated by region
 
foreach (TypeMembers v3Type in lstOldTypes)
{
Type v4Type = v4Types.FirstOrDefault(t => t.FullName == v3Type.FullName);
 
if (v4Type != null && !v4Type.IsEnum)
{
MemberInfo[] v4Mis = v4Type.GetMembers();
 
if (v4Mis.Length != v3Type.MemberNames.Count)
{
 

MemberInfo [] v4NewMis = v4Mis. Where (mi =>

! V3Type. MemberNames. Contains (mi. Name). ToArray ();

 
result.Add(new TypeMembers()
{
FullName = v4Type.FullName,
MemberNames = v4NewMis.Select(mi => mi.Name).ToList()
});
}
}
}
 
#endregion
 
# All newly added types of region
 
List<string> v3TypeFullName = lstOldTypes.Select(tm => tm.FullName).ToList();
 
 

Type [] v4NewTypes = v4Types. Where (t =>! V3TypeFullName. Contains (t. FullName )&&

! T. IsEnum). ToArray ();

 
foreach (Type v4NewType in v4NewTypes)
{
result.Add(new TypeMembers()
{
FullName = v4NewType.FullName,
MemberNames = v4NewType.GetMembers().Select(mi => mi.Name).ToList()
});
}
 
#endregion
 
return result.OrderBy(tm=>tm.FullName).ToList();
}
}
}
 

 

The implementation of the service is similar to that of the first reflection version.

 

The Web. config file binding code is as follows:

 

 
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="NewBinding0" maxReceivedMessageSize="65536000" />
</wsHttpBinding>
<mexHttpBinding>
<binding name="NewBinding1" />
</mexHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="V4WcfService.Service1Behavior"
name="V4WcfService.Service1">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="NewBinding0"
contract="V4WcfService.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="NewBinding1"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="V4WcfService.Service1Behavior">
<! -- To avoid metadata leakage, set the following value to false before deployment and delete the above metadata endpoint -->
<serviceMetadata httpGetEnabled="true"/>
<! -- To receive fault exception details for debugging, set the following value to true. Set it to false before deployment to avoid leakage of exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
 

 

Because the default maxcompute edmessagesize is 65536, two zeros are added to the backend. Otherwise, a classic exception out of range is thrown.

 

 

 

 

 

The WinForm program interface is as follows:

 

The background code is as follows:

 
namespace V4NewLooker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
 
private List<TypeMembers> NewTypeMembers { get; set; }
 
private void btnSearch_Click(object sender, EventArgs e)
{
Assembly mscorlibAssembly = typeof(object).Assembly;
 
List<TypeMembers> v3TypeMembers = new List<TypeMembers>();
 
 
foreach (Type v4NewType in mscorlibAssembly.GetTypes())
{
List<string> memberNames = new List<string>();
MemberInfo[] mis = v4NewType.GetMembers();
foreach (MemberInfo mi in mis)
{
memberNames.Add(mi.Name);
}
 
v3TypeMembers.Add(new TypeMembers()
{
FullName = v4NewType.FullName,
MemberNames = memberNames
});
}
 
using (Service1Client client = new Service1Client())
{
NewTypeMembers = client.GetNewTypeMember(v3TypeMembers);
}
 
List<string> typeNames=new List<string>();
foreach (TypeMembers tm in NewTypeMembers)
{
typeNames.Add(tm.FullName);
}
 
lstBox_Types.DataSource = typeNames;
 
}
 
private void lstBox_Types_SelectedIndexChanged(object sender, EventArgs e)
{
string fullName = lstBox_Types.SelectedItem.ToString();
foreach (TypeMembers tm in NewTypeMembers)
{
if (tm.FullName == fullName)
{
lstBox_Members.DataSource = tm.MemberNames;
 
break;
}
}
}
}
}
 

 

The function of the Search button is to encapsulate all types of the current Framework 2.0 and MemberInfo in all types into requests, and then call the WCF Service. The service outputs new methods and classes based on the passed Type and MemberInfo.

 

 

 

 

The running effect is as follows:

 

We can see that the ReadLines. AppendAllLines method is added to the 4.0 File class. The preceding two ReadLines are shown because the ReadLines method is overloaded by LoveJenny.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.