Reputation: 11
Here I have class "test".
public class test
{
public int a { get; set; }
public int b { get; set; }
public int c { get; set; }
public int d { get; set; }
}
void call1(test obj )
{
// Question: I need to exclude the property a. which means "test.a" should not able to access or view.
}
void call2(test obj )
{
// I need to exclude the property both a & b
return;
}
Upvotes: 1
Views: 1049
Reputation: 2518
You can try with two methods and new, you return an anonymous object which is a "partial" view of test.
public class test
{
private int a { get; set; }
private int b { get; set; }
private int c { get; set; }
private int d { get; set; }
public object testWithoutA()
{
var test = new
{
this.b,
this.c,
this.d
};
return test;
}
public object testWithoutAAndB()
{
var test = new
{
this.c,
this.d
};
return test;
}
}
UPDATE: The question concerns incoming data from WCF REST PUT
Assuming that you are working with stored procedures, why not create 2 stored procedures which ignore in one case 'a' and 'a' and 'b'?
Upvotes: 0
Reputation: 359
You can not change the structure of an object in run time.
but there are plenty of ways to prevent accessing a property of an instance, for example see this codes:
public class Test
{
// define some private varibales:
private int _a;
private int _b;
private bool accessA = true;
private bool accessB = true;
public int a
{
get
{
if (accessA)
{
return _a;
}
else
{
throw new Exception("At this moment this property was excluded.");
}
}
set
{
if (accessA)
{
_a = value;
}
else
{
throw new Exception("At this moment this property was excluded.");
}
}
}
public int b
{
get
{
if (accessB)
{
return _b;
}
else
{
throw new Exception("At this moment this property was excluded.");
}
}
set
{
if (accessB)
{
_b = value;
}
else
{
throw new Exception("At this moment this property was excluded.");
}
}
}
public int c { get; set; }
public int d { get; set; }
public void ExcludeA()
{
accessA = false;
}
public void ExcludeB()
{
accessB = false;
}
}
public void call1(Test obj)
{
//do some work here ....
obj.ExcludeA();
}
public void call2(Test obj)
{
// do some work here ...
obj.ExcludeA();
obj.ExcludeB();
}
Upvotes: 0
Reputation: 186668
You can use interfaces here:
public interface IRestrictedNoAandB {
int c { get; set; }
int d { get; set; }
}
public interface IRestrictedNoA: IRestrictedNoAandB {
int b { get; set; }
}
public class test: IRestrictedNoA {
public int a { get; set; }
public int b { get; set; }
public int c { get; set; }
public int d { get; set; }
}
// You can't access property "a" within "call1" method
void call1(IRestrictedNoA obj ) {...}
// You can't access properties "a" and "b" within "call2" method
void call2(IRestrictedNoAandB obj ) {...}
Upvotes: 2
Reputation: 12705
you cant do this in OOP. once a property is public it will be accessible to everybody. What instead you can do is create to models like
public class test1
{
//public int a { get; set; }
public int b { get; set; }
public int c { get; set; }
public int d { get; set; }
}
public class test2
{
//public int a { get; set; }
//public int b { get; set; }
public int c { get; set; }
public int d { get; set; }
}
and use these in the two methods mentioned. Then use an object mapper like Glue
or ObjectMapper
to automatially map from test to test1 and test2
but using this you might have to change the structure of your program a little bit and it would be awsm if you return test1 and test2 instead of void and then change the values of the main test instance.
Upvotes: 0