开发者

c# class property with 'internal' set accessor becomes invisible

public class Request
{
    public string ID { get; internal set; }
    public int TaskID { get; set; }

    public Request()
    {
        ID = System.Guid.NewGuid().ToString();
    }
}

On the client side I only see TaskID property. I don't get it why the 'ID' property is not visible? If I remove 'internal' it works. But I need it to be internal.

My bad, I should have mentioned it that it's WCF.


Thanks for all the answers开发者_JAVA百科. I realized that what I'm trying is not possible.


Why you say client side, do you mean you're returning the class from a web service? If that is the case, .NET requires both the getter and setter to be public for the property to be included in XML serialization operations.


Assuming your request class is a DataContract, all properties must have a public getter and setter. Otherwise construction of the class on the "other" side is not possible. Thus it ca not be a DataContract.


By using Internal modifier it can be accessible only: By any code in the same assembly, but not from another assembly.

And if you are reference it in derived class than use

Protected internal

Anoteher Way is

You need to make the assembly's friends with the InternalsVisibleTo attribute.

Assuming that you don't sign your assemblies, it's as easy as adding an assembly level InternalsVisibleTo attribute to the C# project with the name of the VB.Net assembly. Typically you do this in AssemblyInfo.cs (under the Properties folder)

[assembly:InternalsVisibleTo("MyVbAssemblyName")]


Assuming you are using WCF, the proxy for this object will only have items with both public getters and setters. You can't really get around this, because in deserializing the object the server-side proxy will need to call the setter. If you aren't worried about deserialization, you can create a setter that does nothing and make it public, and make an internal function that acts as the real setter.


ok; assuming this is a service, a workaround would be to give the client some sort of hashed token rather than the real ID, then, upon receiving a request back from the client, you could lookup the real ID by token, or throw an exception if they changed it.


If you remove the internal modifier, the setter will have public visibility. Internal elements are visible to types located in the same assembly, so if your client is in another assembly, the setter will not be visible.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜