Freigeben über


Codeausschnitt: Festlegen des Parameters-Objekts vor dem Aufrufen von "GenericInvoker"

Letzte Änderung: Donnerstag, 6. Mai 2010

Gilt für: SharePoint Server 2010

Im folgenden Codebeispiel wird gezeigt, wie Sie das Parameters-Objekt festlegen, bevor Sie GenericInvoker aufrufen.

Voraussetzungen:

  • Microsoft SharePoint Server 2010 oder Microsoft SharePoint Foundation 2010.

  • Microsoft .NET Framework 3.5 und Microsoft Visual Studio.

So verwenden Sie dieses Beispiel

  1. Starten Sie Visual Studio, und erstellen Sie ein C#-Konsolenanwendungsprojekt. Wählen Sie beim Erstellen des Projekts .NET Framework 3.5 aus.

  2. Klicken Sie im Menü Ansicht auf Eigenschaftenseiten, um die Projekteigenschaften aufzurufen.

  3. Wählen Sie auf der Registerkarte Build unter Zielplattform die Option Beliebige CPU aus.

  4. Schließen Sie das Fenster mit den Projekteigenschaften.

  5. Entfernen Sie im Projektmappen-Explorer unter Verweise sämtliche Projektverweise bis auf System und System.Core.

  6. Fügen Sie dem Projekt die folgenden Verweise hinzu:

    1. Microsoft.BusinessData

    2. Microsoft.SharePoint

    3. System.Data

    4. System.Web

    5. System.Xml

  7. Ersetzen Sie den automatisch generierten Code in Program.cs durch den Code am Ende dieses Verfahrens.

  8. Ersetzen Sie siteUrl, nameSpace, entityName und methodInstanceName durch gültige Werte.

  9. Bearbeiten Sie den Code für die Schritte 4, 5 und 6 nach Bedarf. Die in den Schritten 4, 5 und 6 eingegebenen Daten müssen mit den Parametern und Typen übereinstimmen, die für die Methode der generischen aufrufenden Instanz im BDC-Modell definiert sind.

  10. Drücken Sie F6, um die Lösung zu erstellen.

  11. Drücken Sie STRG+F5, um das Beispiel auszuführen.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using System.Web;
using Microsoft.BusinessData.MetadataModel.Collections;
using Microsoft.BusinessData.Infrastructure;

namespace Microsoft.SDK.Sharepoint.Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteURL = "<siteUrl>";
            string entityNS = "<nameSpace>";
            string entityName = "<entityName>";
            string methodInstanceName = "<methodInstanceName>";
            using (SPSite site = new SPSite(siteURL))
            {
                using (new Microsoft.SharePoint.SPServiceContextScope(
                    SPServiceContext.GetContext(site)))
                {
                    BdcService service = 
                        SPFarm.Local.Services.GetValue<BdcService>
                        (String.Empty);
                    IMetadataCatalog catalog = 
                        service.GetDatabaseBackedMetadataCatalog(
                        SPServiceContext.Current);                                      

                    IEntity entity = 
                        catalog.GetEntity(entityNS, entityName);
                    ILobSystemInstance LobSysteminstance = 
                        entity.GetLobSystem().
                        GetLobSystemInstances()[0].Value;

                    // Step 1: Obtain the MethodInstance from Entity.
                    IMethodInstance myGenericInvoker = 
                        entity.GetMethodInstance(
                        methodInstanceName, 
                        MethodInstanceType.GenericInvoker);

                    // Step 2: Obtain the Parameters using the Method.
                    IParameterCollection parameters = 
                        myGenericInvoker.GetMethod().GetParameters();

                    // Step 3: Create the Object[] that needs 
                    // to be passed in.
                    Object[] rawParameters = 
                        new Object[parameters.Count];

                    // Step 4: For simple types and known types you can 
                    // just create the instance and assign it to the 
                    // parameter. 
                    // Known types are the types that are known to you at 
                    // compile time. These are the types that are in .NET Framework,              
                    // BDC, or assemblies in the global assembly cache. The types that are 
                    // defined in your proxy are NOT known types, as BDC 
                    // will generate another proxy for the Web service. 
                    rawParameters[0] = 32;
                    rawParameters[1] = "A";
                    rawParameters[2] = new System.Data.DataTable("X");

                    // Step 5: For "unknown types" (such as types defined 
                    // in your proxy), you need to access to the type 
                    // reflector to instantiate the complex types 
                    // defined in your proxy.
                    ITypeReflector reflector = parameters[3].TypeReflector;
                    ITypeDescriptor rootTypeDescriptor = 
                        parameters[3].GetRootTypeDescriptor();
                    Object instance = reflector.Instantiate(
                        rootTypeDescriptor);
                    // Or if you want to have the instance object 
                    // use default values 
                    // defined in your model, use the following:
                    // Object instance = 
                    //     reflector.Instantiate(
                    //         rootTypeDescriptor, myGenericInvoker); 
                    // Or if the root of the parameter is a collection, 
                    // use the following:
                    // Object instance = 
                    //     reflector.Instantiate(rootTypeDescriptor, 55); 
                    // Or if the root of the parameter is a collection 
                    // and you want to use default values, 
                    // use the following: 
                    // Object instance = 
                    //     reflector.Instantiate(
                    //     rootTypeDescriptor, 55, myGenericInvoker); 

                    rawParameters[3] = instance;

                    // Step 6: To set values in the complex object 
                    // you’ve just created, you can use the Set
                    // method on TypeReflector.
                    ITypeDescriptor child = 
                        rootTypeDescriptor.GetChildTypeDescriptors()[3];
                    // Alternatively you can look over them and 
                    // check the name, 
                    // but since you are using generic invoker, 
                    // it is most likely that you have intimate 
                    // knowledge of the method.
                    // If your object is a structure:
                     reflector.Set(
                        child, rootTypeDescriptor, ref instance, "value"); 
                    // Or if your object is a collection:
                    // reflector.Set(
                    //     child, rootTypeDescriptor, ref instance, 
                    // "value", /*index*/ 2); 
                    // If you want to set values in deeper parts of 
                    // the structure, you’ll need to navigate to the 
                    // relevant type descriptors, 
                    // and provide the corresponding root object, 
                    // parent type descriptor and child type descriptor. 
                    // You can also create instances of deeper 
                    // TypeDescriptors and assign the created instances 
                    // to parts of the structure using the Set method.

                    // NOTE: Repeat steps 5 and 6 for all the values 
                    // you need to create and set.

                }
            }
        }
    }
}

Als hilfreiche Abkürzung können Sie IMethodInstance.GetMethod().CreateDefaultParameterInstances(IMethodInstance) verwenden. Damit wird ein Object[] mit vollständig instanziierten Back-End-Argumenten zurückgegeben, die auf Standardwerten des Modells basieren. Dann werden diese Standardwerte mithilfe von TypeReflector wie im folgenden Code gezeigt außer Kraft gesetzt oder geändert. CreateDefaultParameterInstances() ist eine Abkürzung für die oben genannten Schritte 3 bis 5.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using System.Web;

namespace Microsoft.SDK.Sharepoint.Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteURL = ""<siteUrl>";
            string entityNS = "<nameSpace>";
            string entityName = "<entityName>";
            string methodInstanceName = "<methodInstanceName>";
            using (SPSite site = new SPSite(siteURL))
            {
                using (new Microsoft.SharePoint.SPServiceContextScope(
                    SPServiceContext.GetContext(site)))
                {
                    BdcService service = 
                        SPFarm.Local.Services.GetValue<BdcService>(
                        String.Empty);
                    IMetadataCatalog catalog = 
                        service.GetDatabaseBackedMetadataCatalog(
                        SPServiceContext.Current);

                    IEntity entity = catalog.GetEntity(
                        entityNS, entityName);
                    ILobSystemInstance LobSysteminstance = 
                        entity.GetLobSystem().GetLobSystemInstances()
                        [0].Value;

                    // Step 1: Obtain the MethodInstance from the entity.
                    IMethodInstance myGenericInvoker = 
                        entity.GetMethodInstance(
                        methodInstanceName, MethodInstanceType.GenericInvoker);

                    // Step 2: Obtain the Parameters using the Method.
                    IParameterCollection parameters = 
                        myGenericInvoker.GetMethod().
                        CreateDefaultParameterInstances(IMethodInstance);
                }
            }
        }
    }
}

Siehe auch

Referenz

BdcService

Services

IMetadataCatalog

GetDatabaseBackedMetadataCatalog(SPServiceContext)

GetEntity(String, String)

IEntity

GetLobSystem()

GetLobSystemInstances()

ILobSystemInstance

GetMethodInstance(String, MethodInstanceType)

IMethodInstance

IParameterCollection

CreateDefaultParameterInstances(IMethodInstance)