Below error while deserializing result set(tuple) in WCF function.
There was an error while trying to deserialize paramenter XXX. Please see InnerException for more details.
'System.Tuple' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.
Below is the tuple definition.
Public Class XXX
Public Property aaa As New List(of Tuple(of bbb,ccc))
End class
Untimely, you need to make a class instead of using a Tuple. The exception says you cannot use a Tuple because A) It's not marked with the DataContract Attribute and B) Because it doesn't have a parameter-less constructor. So, a Tuple<int,string> cannot be set to a new Tuple<int,string>();
If the class you sent via WCF was like this...
public class MyClassToSendThroughWcf
{
public tuple<int,string> MyData {get;set;} //The tuple here is problem, replace with the class below...
}
You could replace the type of "MyData" with a class that does the same thing like this...
public class MyClassToUseWithWcf
{
public MyClassToUseWithWcf(){}
public MyClassToUseWithWcf(int i, string s)
{
Item1 = i;
Item2 = s;
}
public int Item1 {get;set;}
public string Item2 {get;set;}
}
Now, both classes have default constructors, so it can be serialized. I suggest avoiding tuples anyway.
Related
I have a class Airplane:
class Airplane {
private:
int value;
public:
// some public functions that arent relevant
};
and then another class that derives from Airplane
class Model : public Airplane {
private:
bool flag;
public:
// some functions
};
What I need to do is create a pointer of type airplane that points to the child class, aka model.
Like so: Airplane* A = new Model();
Then I can use A->functions to access the public functions of airplane to modify airplanes private data members.
My problem is I need to modify the private data members of Model. I created public member functions in model to change models private members, but it doesnt let me do A->modelfunctions. Is there a way to modify the Model private members with my system above?
My instructions were to create a dynamic instance of a Model and hold the address in an Airplane pointer. I want to do this while being able to modify private variables from both classes, either directly or through public functions. Right now I can modify "value" from airplane, but not "flag" from model using the Airplane* A = new Model(); system.
Thank you.
So I got the following Structure.
abstract class Item {}
abstract class ICarriable {}
class Apple : public Item {}
class Stick : public Item, public Carriable {}
// More Items which may or may not derrive from Carriable
class Pants : public Carriable {}
Stick* StickyStick = new Stick();
Pants* CoolPants = new Pants();
void Equip(ICarriable* ItemToEquip) {
// Do things here
}
Equip(StickyStick); // Allowed.
Equip(CoolPants); // Not Allowed.
Now I want a function where I can pass Items which implements Carraible. So that I can pass Stick but not an Apple.
I try to avoid an if inside of that function in case the Item cant be cast to Carriable.
To me it's not clear why you would want this and I think it is a design flaw. Why would an Equip function that only uses the Carriable interface functions why does it need the parameter object to also be an Item? Does the function need Item specific functions? If you want it to just satisfy the requirement of both being an Item and Carriable you could just make a third class like 'Equipable' and let that inherit from both.
I have a class, describing some object with properties, e.g. inventory item.
And along with default constructor, I need parametrized constructor, to create items with certain set of parameters, for example, size)
UCLASS()
class xxx_API UItem : public UObject
{
GENERATED_BODY()
public:
UItem();
~UItem();
UItem(int _size);
UPROPERTY(BlueprintReadOnly, Category = "Parameters")
int size = 0;
};
And I have another class, which I want to serve as container for pre defined items, so I can have references to it from other places of the game.
UCLASS()
class xxxx_API UItemsContainer : public UObject
{
GENERATED_BODY()
public:
UItemsContainer();
~UItemsContainer();
UItem SomeItem = UItem(100);
};
So I can at the begiining of game create this item container
ItemsContainer = NewObject<UItemsContainer>();
And then add it to some item collection of certain entity like this
TArray<UItem*> CharacterItems = {};
CharacterItems.Add(&ItemsContainer.SomeItem);
Since items are fixed and will not change during game, I dont want to create specific object for each of them, and just have references to container entries.
Though on compilation I get error, that I try to access private members of UItem class. Though, it's constructor is public and all properties are public. I think that there is something in UClass, that dont allow me to use constructor that way, since I can do this with objects, that are not UObject. But I need it to be UObject, to be usable in blueprints. And I dont know how to call non-default constructor in other way.
Probably I can create default object with NewObject, and then initialize it and store in array, but looks like there will be too much code and complication.
Any suggestions please?
Because C++ has been stylized in Unreal, e.g. auto generated source file of UItem is in the directory:
MyProj\Intermediate\Build\Win64\UE4Editor\Inc\MyProj\Item.generated.h
You can open this source, the copy-constructor of UItem has been shadowed, and UItem SomeItem = UItem(); would trigger copy-constructor, so you will get a compilation error that try to access private members of UItem class.
In Unreal, you must invoke NewObject when creating UObject, e.g.
SomeItemPtr = NewObject<UItem>(Outer);
If you want to pass parameters on creating UObject, you can define a specific function
void UItem::SetParams(int Size)
{
//initiation ...
}
e.g.
UItemsContainer::AddItem(int ItemSize)
{
SomeItemPtr = NewObject<UItem>(this);
SomeItemPtr->SetParams(ItemSize);
}
I have three classes A, B and C Where A is abstract class, B is also an abstract class and B extends A and C is non-abstract which extends B. I have variable by name AddressService addressService which is public in class B and is private in class A. I am writing a test case for class C which is non-abstract and calling method which is in class A and in class A i need to set the value of AddressService addressService variable which is used to invoke a method. AddressService is an interface which has methods and i am invoking one of the method in my super class A. Following is my code
public interface AddressService{
void test();
}
public abstract class A{
private AddressService addressService = (AddressService) ServiceLocatorBeanFactory.getService(AddressService.class);
public void createDocument(){
addressService . test();
}
}
public abstract class B extends A{
public AddressService addressService = (AddressService) ServiceLocatorBeanFactory.getService(AddressService.class);
}
public class C extends B {
}
Here is my test class
#RunWith(PowerMockRunner.class)
#PrepareForTest({ServiceLocatorBeanFactory.class})
public class createTest {
#SuppressWarnings("unchecked")
#Test
public void createTurnaroundDocument() throws Exception{
PowerMockito.mockStatic(ServiceLocatorBeanFactory.class);
AddressService addressService = Mockito.mock(AddressService.class);
PowerMockito.when(ServiceLocatorBeanFactory.getService(AddressService.class)).thenReturn(addressService);
C original = new C();
C handler = PowerMockito.spy(original);
handler.createDocument();
}
}
I tried setting the value of AddressService addressService present in class A which is abstract in multiple ways but still the value is null and i get NullPointer Exception.
Following are the different ways
1.Whitebox.setInternalState(handler, AddressService.class, addressService);
When i set this way the value is not set and is null and gives me NullPointerException
2.MemberModifier.field(A.class, "addressService").set(A.class, addressService);
When i set this way i get the following exception
java.lang.IllegalArgumentException: Can not set AddressService field A.addressService to java.lang.Class
3.Whitebox.setInternalState(A.class, AddressService.class, addressService);
When i set this way i get the following exception
org.powermock.reflect.exceptions.FieldNotFoundException: No static field assignable from "AddressService" could be found in the class hierarchy of A.
4.Whitebox.setInternalState(A.class, "addressService", addressService);
when i set this way i get the following exception
org.powermock.reflect.exceptions.FieldNotFoundException: No static field named "addressService" could be found in the class hierarchy of A.
Is there a way where we can set the value of private variable present in the super class using Mockito or PowerMockito. Please help
MemberModifier.field(A.class, "addressService").set(A.class, addressService);
This is wrong: java.lang.reflect.Field.set needs an instance of your class if you want to set a non-static method, not the class. Might still not work, perhaps you will have to use setAccessible(true) on the field first:
Field field = MemberModifier.field(A.class, "addressService");
field.setAccessible(true);
field.set(c, addressService);
You can do the first line with pure reflection anway...
Field field = A.class.getDeclaredField( "addressService" );
field.setAccessible(true);
field.set(c, addressService);
But of course, the big mystery is: Why do you have the same variable twice? Smells like bad code. Make the inner one protected instead of static, it's very unlikely that something good will come from having two.
I want to use a function from my Page class but I do not want to inherit from it. Is there another way I can use theonPage(vector<string> vec) function in my element and elementX class without using inheritance? Would association Work? I was thinking that.
Code is below:
class Page
{
string str;
vector<string>::iterator it;
void onPage(vector<string>vec);
};
class element
{
Page p;
};
class elementX : public element
{
};
Declare it public, so foreign classes can access it.
class Page
{
string str;
vector<string>::iterator it;
public:
void onPage(vector<string>vec);
};
(you can use public, private and protected to control access to class members, make sure to read them all up. private is the default for classes).
Yes you can. The issue I can see is that you did not declare the void onPage(vector<string>vec); function as public (place public: before the function), since class members are private (hidden for other classes) in C++ by default.