开发者

C# How to cast a strong datatype?

I hope some of you guys can help me with this problem....

I have a class library containing amongst other things 1 complex class. this class library is used in 2 other projects in the solution. 1 = Console Application , 2 = Web Service Application (Web Site Application).

In the console application I have created an asmx web service reference, which passes my complex class as the 1 parameter in a method call.

The class is of type : ScheduleSummaryTransport

The function tha开发者_运维百科t does the work in the console app is expecting the object bound from the class library DLL, and not from the web service reference. However the web service call is expecting the parameter of type... from the web service reference.

So how can I cast

ClassLibrary.ScheduleSummaryTransport -> WebService.ScheduleSummaryTransport ?

I've tried:

wsReporting.SendReportSummary( (Reporting.ScheduleSummaryTransport) scheduleSummary);

But in visual studio its detected a design time error : Cannot cast expression.....

Please assist, and thanks

Update

For completeness more code:

/// <summary>
        /// Sends the schedule report via email.
        /// </summary>
        /// <param name="scheduleSummary">
        /// The schedule summary.
        /// </param>
        private static void SendScheduleReport(ScheduleSummaryTransport scheduleSummary)
        {
            if (!Convert.ToBoolean(ConfigurationManager.AppSettings["EmailSummary"]))
            {
                return;
            }

            Trace.WriteLine(string.Empty);
            Trace.WriteLine("Sending summary by email... please wait ");

            Reporting.Reporting wsReporting = new Reporting.Reporting { Timeout = -1 };
            wsReporting.SendReportSummary( (Reporting.ScheduleSummaryTransport) scheduleSummary);
            Trace.WriteLine("Done...");
        }


The WebService Proxy generator (wsdl.exe) generates a class for you to hold all information about objects transmitted via the webservice. This new class conflicts with the class referenced in the client.

You need to get rid of the auto-generated classes. To do this, show all files in your solution explorer (Icon at the top). There will be a class Reference.cs as a child of your Webservice Reference, where you can find the autogenerated code. Add a using ClassLibrary there and delete all code concerning the duplicate classes.

Worked for me. =)


If they're unrelated types, you can't cast between them. It's like trying to cast from String to MemoryStream - what would you expect to happen? The compiler knows that would never work, so it stops you from trying it.

The problem here is having multiple types in the first place. Avoid that if you can possibly do so. If you can't, you'll need to write some sort of conversion method two convert between the two types.


I'm still unclear why you have two different versions of the same class but if you can't get to where you're referencing and using the version in the class library in both places, you might want to check out Automapper to handle mapping from one version to the other.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜