The Wayback Machine - https://web.archive.org/web/20160118150300/https://msdn.microsoft.com/en-us/library/aa480010.aspx
Generics FAQ: Tool Support
Abortable Thread Pool
The Analytic Hierarchy Process
API Test Automation in .NET
Asynchronous HttpWebRequests, Interface Implementation, and More
Bad Code? FxCop to the Rescue
Basics of .NET Internationalization
Behind the Scenes: Discover the Design Patterns You're Already Using in the .NET Framework
BigInteger, GetFiles, and More
Binary Serialization of DataSets
Building Voice User Interfaces
Can't Commit?: Volatile Resource Managers in .NET Bring Transactions to the Common Type
CLR Inside Out: Base Class Library Performance Tips and Tricks
CLR Inside Out: Ensuring .NET Framework 2.0 Compatibility
CLR Inside Out: Extending System.Diagnostics
CLR Profiler: No Code Can Hide from the Profiling API in the .NET Framework 2.0
Concurrent Affairs: Build a Richer Thread Synchronization Lock
Custom Cultures: Extend Your Code's Global Reach With New Features In The .NET Framework 2.0
Cutting Edge: Collections and Data Binding
Const in C#, Exception Filters, IWin32Window, and More
Creating a Custom Metrics Tool
DataGridView
DataSets vs. Collections
Determining .NET Assembly and Method References
Experimenting with F#
File Copy Progress, Custom Thread Pools
Finalizers, Assembly Names, MethodInfo, and More
Got Directory Services?: New Ways to Manage Active Directory using the .NET Framework 2.0
High Availability: Keep Your Code Running with the Reliability Features of the .NET Framework
How Microsoft Uses Reflection
ICustomTypeDescriptor, Part 2
ICustomTypeDescriptor, Part 1
Iterating NTFS Streams
JIT and Run: Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects
Lightweight UI Test Automation with .NET
Low-Level UI Test Automation
Make Your Apps Fly with the New Enterprise Performance Tool
Managed Spy: Deliver The Power Of Spy++ To Windows Forms With Our New Tool
Memory Models: Understand the Impact of Low-Lock Techniques in Multithreaded Apps
Microsoft Java Virtual Machine Update
Microsoft .NET Framework Delivers the Platform for an Integrated, Service-Oriented Web, Part 2
Mini Dump Snapshots and the New SOS
Mutant Power: Create A Simple Mutation Testing System With The .NET Framework
NamedGZipStream, Covariance and Contravariance
.NET Internationalization Utilities
.NET Profiling: Write Profilers With Ease Using High-Level Wrapper Classes
No More Hangs: Advanced Techniques To Avoid And Detect Deadlocks In .NET Apps
The Perfect Host: Create and Host Custom Designers with the .NET Framework 2.0
Phoenix Rising
Scheme Is Love
Security Enhancements in the .NET Framework 2.0
Sepia Tone, StringLogicalComparer, and More
Software Testing Paradoxes
Stay Alert: Use Managed Code To Generate A Secure Audit Trail
Stream Decorator, Single-Instance Apps
StringStream, Methods with Timeouts
SUPERASSERT Goes .NET
Tailor Your Application by Building a Custom Forms Designer with .NET
Test Harness Design Patterns
ThreadPoolPriority, and MethodImplAttribute
ThreadPoolWait and HandleLeakTracker
Three Vital FXCop Rules
A Tidal Wave of Change
To Confirm is Useless, to Undo Divine
Touch All the Bases: Give Your .NET App Brains and Brawn with the Intelligence of Neural Networks
Transactions for Memory
Trustworthy Software
Tune in to Channel 9
UDP Delivers: Take Total Control Of Your Networking With .NET and UDP
UI on the Fly: Use the .NET Framework to Generate and Execute Custom Controls at Run Time
Unexpected Errors in Managed Applications
Unhandled Exceptions and Tracing in the .NET Framework 2.0
Using Combinations to Improve Your Software Test Case Generation
Wandering Code: Write Mobile Agents In .NET To Roam And Interact On Your Network
What Makes Good Code Good?
XML Comments, Late-bound COM, and More
TOC
Collapse the table of content
Expand the table of content

Generics FAQ: Tool Support

 

Juval Lowy

October 2005

Applies to:
   Generic Types
   Microsoft Visual Studio 2005

Summary: Review frequently asked questions regarding generic types and their various uses. (5 printed pages)

Contents

How Does Visual Studio 2005 Support Generics?
Can I Data-Bind Generic Types to Windows and Web Data Controls?
How Are Web Service Proxies Created for Generic Types?

How Does Visual Studio 2005 Support Generics?

Visual Studio 2005 supports generics well. InteliSense displays correctly the generic types, implementing generic interfaces is just as easy as with non-generic interfaces. The most impressive aspect of support is in the debugger, which displays the correct type arguments information when hovering over generic types.

Can I Data-Bind Generic Types to Windows and Web Data Controls?

Yes. All the generic collections also support the non-generic collection interfaces, and you can use them as data sources to bind to controls just as with the non-generics collections.

For example, consider a Windows Forms form that has a combobox called m_ComboBox. You can assign into as a data source the List<T> collection:

[C#]

partial class MyForm : Form
{
   void OnFormLoad(object sender, EventArgs e)
   {
      List<string> cities = new List<string>();
      cities.Add("New York");
      cities.Add("San Francisico");
      cities.Add("London");

      m_ComboBox.DataSource = cities;
   }
}

[Visual Basic]

Public Class MyForm 
   Inherits Form
   Private Sub OnFormLoad(ByVal sender As System.Object,
                       ByVal e As System.EventArgs) Handles MyBase.Load

      Dim cities As New List(Of String)
      cities.Add("New York")
      cities.Add("San Francisico")
      cities.Add("London")
      m_ComboBox.DataSource = cities
   End Sub
End Class

[C++]

public ref class MyForm : public Form
{
   void Form_Load(Object^ sender,EventArgs^ e)
   {
      List<String ^> ^cities = gcnew List<String ^>;
     cities->Add("New York");
     cities->Add("San Francisico");
     cities->Add("London");
     m_ComboBox->DataSource = cities;
   }
};

How Are Web Service Proxies Created for Generic Types?

The web service proxy class generated by Visual Studio 2005 does not necessarily maintain affinity to the returned types from a web service. The proxy class will contain values corresponding only to the serialized representation of the generic types only.

As mentioned in the question on generics and web services, for this definition of a web service:

[C#]

public class MyWebService 
{
   [WebMethod]
   public List<string> GetCities() 
   {
      List<string> cities = new List<string>();
      cities.Add("New York");
      cities.Add("San Francisco");
      cities.Add("London");
      return cities;
   }
}

[Visual Basic]

Public Class MyWebService
    [WebMethod]
    Public Function GetCities() As List(Of String)
        Dim cities As New List(Of String)()
        cities.add("New York")
        Cities.add("San Francisco")
        cities.add("London")
        Return cities
    End Function
End Class

[C++]

public ref class MyWebService 
{
   public:
   [WebMethod]
   List<String ^> ^ GetCities() 
   {
      List<String ^> ^cities = gcnew List<String ^>();
      cities->Add("New York");
      cities->Add("San Francisco");
      cities->Add("London");
      return cities;
   }
}

The returned list will be marshaled as an array of strings. Consequently, the Visual Studio 2005 generated proxy will contain this definition of the GetCities() method:

[C#]

[WebServiceBinding(Name="MyWebServiceSoap")]
public partial class MyWebService : SoapHttpClientProtocol 
{
   public MyWebService() 
   {...}

   [SoapDocumentMethod(...)]
   public string[] GetCities()
   {
      object[] results = Invoke("GetCities",new object[]{});
      return ((string[])(results[0]));
   }
}

[Visual Basic]

<WebServiceBinding(Name:="MyWebServiceSoap")> _
Public Partial Class MyWebService
      Inherits SoapHttpClientProtocol

   Public Sub New()
      ...
   End Sub
   
   <SoapDocumentMethod(...)> _
   Public Function GetCities() As String()
      Dim results As Object() = Invoke("GetCities", New Object(){})
      Return CType(results(0), String())
   End Function
End Class

[C++]

[WebServiceBinding(Name=L"MyWebServiceSoap")]
public ref class MyWebService : public SoapHttpClientProtocol 
{
   public: Service::Service() 
   {...}

   public:[SoapDocumentMethod(...)]
   cli::array<String^  >^ GetCities()
   {
      cli::array<Object^  >^ results = Invoke(L"GetCities",
                             gcnew cli::array<Object^>(0));
      return (cli::safe_cast<cli::array< System::String^>^>(results[0]);
    }   
}

 

About the author

Juval Lowy is a software architect and the principal of IDesign, specializing in .NET architecture consulting and advanced .NET training. Juval is Microsoft's Regional Director for the Silicon Valley, working with Microsoft on helping the industry adopt .NET. His latest book is Programming .NET Components 2nd Edition (O'Reilly, 2005). Juval participates in the Microsoft internal design reviews for future versions of .NET. Juval published numerous articles, regarding almost every aspect of .NET development, and is a frequent presenter at development conferences. Microsoft recognized Juval as a Software Legend as one of the world's top .NET experts and industry leaders.

Show:
© 2016 Microsoft
Morty Proxy This is a proxified and sanitized view of the page, visit original site.