コード例 #1
0
        /// <summary>
        /// Generates a <see cref="DomainService"/> proxy type.
        /// </summary>
        /// <param name="domainServiceContract">The <see cref="DomainService"/> contract type to implement.</param>
        /// <param name="domainService">The <see cref="DomainService"/> type to generate a proxy for.</param>
        /// <returns>Returns a <see cref="DomainService"/> proxy type.</returns>
        public static Type Generate(Type domainServiceContract, Type domainService)
        {
            if (domainServiceContract == null)
            {
                throw new ArgumentNullException("domainServiceContract");
            }

            if (domainService == null)
            {
                throw new ArgumentNullException("domainService");
            }

            // Verify 'domainServiceContract' is actually a public interface.
            if (!domainServiceContract.IsInterface || (!domainServiceContract.IsPublic && !domainServiceContract.IsNestedPublic))
            {
                string message = string.Format(CultureInfo.CurrentCulture, Resource.DomainServiceProxyGenerator_ExpectedPublicType, domainServiceContract);
                throw new ArgumentException(message, "domainServiceContract");
            }

            // Verify 'domainService' is actually a public type.
            if (!domainService.IsPublic && !domainService.IsNestedPublic)
            {
                string message = string.Format(CultureInfo.CurrentCulture, Resource.DomainServiceProxyGenerator_ExpectedPublicType, domainService);
                throw new ArgumentException(message, "domainService");
            }

            // Create a new context
            GenerationContext context = new GenerationContext(domainServiceContract, domainService);

            // Define the proxy type
            DefineProxyType(context);

            // Implement required internal state fields
            ImplementInternalStateFields(context);

            // Implement required internal static fields
            ImplementInternalStaticFields(context);

            // Implement IDisposable
            ImplementIDisposable(context);

            // Implement domainServiceContract (ex: expected to be DomainService operations such as 'GetCustomers(...)')
            GenerateOperationMethods(context);

            // If you build it, They will come.
            Type proxyType = context.CreateType();

#if EMIT_DYNAMIC_ASSEMBLY
            // Save the assembly to disk
            SaveAssembly();
#endif

            return proxyType;
        }