简体   繁体   中英

powershell c# convert comobject to .net

I'm using the following code to retrieve a specific address book from outlook:

$outlook = $(New-Object -ComObject Outlook.Application)
$Session = $outlook.Session
$Session.Logon()
$ab = $Session.AddressLists | ? {$_.Name -eq 'test'}

then attempting to use C# to convert the $ab object to .net using the following:

$source = @"
using Microsoft.Office.Interop.Outlook;
public unsafe static class OAB
{
    public static void List(object Stream) 
    {
       var i = Stream as Microsoft.Office.Interop.Outlook.AddressList; 
       return i;
    }
}
"@
$cp = new-object System.CodeDom.Compiler.CompilerParameters                                                                                                                                                                                                 
$cp.CompilerOptions = "/unsafe"
$cp.ReferencedAssemblies.Add('C:\wkdir\refassem\Microsoft.Office.Interop.Outlook.dll')
Add-Type -TypeDefinition $source -CompilerParameters $cp

The error I receive is:

Since 'OAB.List(object)' returns void, a return keyword must not be followed by an object expression

Disclaimer, I'm a complete newbie to C# so go easy on me!

You have your List function declared as a return type of void but you are returning a value. To fix, just change your function to this:

public static Microsoft.Office.Interop.Outlook.AddressList List(object Stream)

void functions mean that no value is going to be returns. If you do plan on returning a value, then you need to remove void and replace it with the type you want to return.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM