By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,422 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,422 IT Pros & Developers. It's quick & easy.

Cast my custom class into System.String

P: n/a

How can I allow someone to cast my C# class into System.String? Is it
possible in C#? In C++ I used "operator" keyword to mark C++ member
function.
Apr 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
John Smith wrote:
How can I allow someone to cast my C# class into System.String? Is it
possible in C#? In C++ I used "operator" keyword to mark C++ member
function.


You can create an explicit (or even implicit) conversion operator in
C#.

See http://msdn2.microsoft.com/en-us/lib...0a(VS.80).aspx for
examples.

Personally, I'd just override ToString and get people to call that
instead, but it's up to you.

Jon

Apr 13 '06 #2

P: n/a

Thank you very much for your response.

I'll ask you and others one more question which might be a little a bit off
the topic, but I have no other chice, since I have asked this on many
newsgroups and haven't received good answer. This is my case:

I'm writing webervice client using .Net 2.0. I have this class:

[System.Web.Services.WebServiceBindingAttribute(Nam e = "ValidateBinding",
Namespace = "http://example.org/Avtentikacija")]
public class MyWebService : SoapHttpClientProtocol
{
[System.Web.Services.Protocols.SoapDocumentMethodAt tribute("", Use =
System.Web.Services.Description.SoapBindingUse.Lit eral, ParameterStyle =
System.Web.Services.Protocols.SoapParameterStyle.B are)]
public XmlDocument validate(string url,
[System.Xml.Serialization.XmlElementAttribute(Names pace =
"http://www.example.org/PodpisaniDokument")] XmlDocument xmlDocument)
{
this.Url = url;
object[] results = null;
try
{
// ERROR occur at this line :(
results = this.Invoke("validate", new object[] { xmlDocument });
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
return ((XmlDocument)(results[0]));
}
}

and I call it like this:

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
xmlDocument.Load("C:\\request.xml");

MyWebService web = new MyWebService();
xmlDocument =
web.validate("http://localhost:8080/MyWeb/services/Avtentikacija",
xmlDocument);
if (xmlDocument != null)
xmlDocument.Save("C:\\response.xml");

And I got this error message:

System.InvalidOperationException: There was an error generating the XML
document. ---> System.InvalidCastException: Unable to cast object of type
'System.Xml.XmlDocument' to type 'System.String'.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriterMyWebService.Write1_validate(Obj ect[]
p)
at
Microsoft.Xml.Serialization.GeneratedAssembly.Arra yOfObjectSerializer.Serialize(Object
objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String
id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String
id)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle)
at
System.Web.Services.Protocols.SoapHttpClientProtoc ol.Serialize(SoapClientMessage
message)
at System.Web.Services.Protocols.SoapHttpClientProtoc ol.Invoke(String
methodName, Object[] parameters)
at MyWeb.MyWebService.validate(String url, XmlDocument xmlDocument) in
C:\Documents and Settings\I\My Documents\Visual Studio
2005\Projects\MyWeb\MyWeb\Form1.cs:line 152

Why? :(

Interesting is that the same code (Invloke() method) works fine for the auto
generated class from WSDL file by Visual Studio 2005. I've searched for
"string" operator, but there isn't any. I have also displayed ToString()
return value and it returns something like
"SomeNamespace.SomeAutoGeneratedClass". I was expection to see whole XML. I
don't know how .Net code casts those auto generated classes???

This is peace of auto generated class:

[System.Xml.Serialization.XmlRoot("PodpisaniDokumen t",
Namespace="http://www.example.org/PodpisaniDokument")]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Sy stem.Xml",
"2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://www.example.org/PodpisaniDokument")]
public partial class PodpisaniDokumentTip {

}
Apr 13 '06 #3

P: n/a

John Smith wrote:
Thank you very much for your response.

I'll ask you and others one more question which might be a little a bit off
the topic, but I have no other chice, since I have asked this on many
newsgroups and haven't received good answer. This is my case:
Generally creating a new topic for a new question is more advisable,
but anyway...
I'm writing webervice client using .Net 2.0. I have this class: public class MyWebService : SoapHttpClientProtocol
{
public XmlDocument validate(string url,
[System.Xml.Serialization.XmlElementAttribute(Names pace =
"http://www.example.org/PodpisaniDokument")] XmlDocument xmlDocument)
{
this.Url = url;
object[] results = null;
try
{
// ERROR occur at this line :(
results = this.Invoke("validate", new object[] { xmlDocument });


Don't you mean

results = this.Invoke("validate", new object[] { url, xmlDocument } );

?

--
Larry Lard
Replies to group please

Apr 13 '06 #4

P: n/a

Thank you very much for your response. :> You were very close.
Don't you mean

results = this.Invoke("validate", new object[] { url, xmlDocument } );


Although VS2005 auto generated classes doesn't invoke web service with 2
arguments, I have tried your example. The only problem is that it adds
<url></url> tags in my <soap:body> request and I don't want this, because
server can't parse it. Is there any way to remoce <url></url> tags?
Apr 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.