Checking whether all my classes have the Serializable attribute - c#

In my current software I make use of serialization and therefore need everything to be marked with the [Serializable] attribute.
Is there an easy way of checking this using my Visual Studio without going through them one at a time, or just waiting for it to crash?
To clarify, I don't need to know how to check if a class is serializable in code. I'm talking about using the IDE.

If you want to use reflection to find classes not marked [Serializable] you can use reflection to get the class types via GetTypes and then find only those not marked with Serializable.
try this:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ReflectOnSerializableAttr
{
class Program
{
static void Main(string[] args)
{
//use Linq
var q = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass && ((t.Attributes & TypeAttributes.Serializable) != TypeAttributes.Serializable)
select t;
q.ToList().ForEach(t => Console.WriteLine(t.Name));
Console.ReadKey();
}
}
[Serializable]
public class TestSerializableOne
{
public string SomeFunc() { return "somefunc"; }
}
public class TestForgotSerializable
{
private int _testInt = 200;
}
}
The above program outputs:
Program
TestForgotSerializable

Related

Do derived classes still depend on the imports of the parent class, even if the method referencing another assembly is being overridden?

I have a parent class that references an outside assembly (AutoCAD)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.EditorInput;
namespace WBPlugin
{
public class DoubleInputRetriever : IUserInputRetriever<Double>
{
public double getUserInput(string prompt)
{
return getUserInput(prompt, 16);
}
public virtual double getUserInput(String prompt, Double defaultValue)
{
Editor ed = Active.Editor;
PromptDoubleOptions pdo = new PromptDoubleOptions(prompt);
pdo.DefaultValue = defaultValue;
pdo.AllowNone = true;
pdo.AllowNegative = false;
PromptDoubleResult pdr = ed.GetDouble(pdo);
if (pdr.Status != PromptStatus.OK)
{
ed.WriteMessage("\n*Cancel*");
return 0;
}
if (pdr.Status == PromptStatus.None)
{
return defaultValue;
}
return pdr.Value;
}
}
}
And then a child class which I made in an attempt to be able to feed in "fake" data to my tool to be able to unit test it outside of AutoCAD.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WBPlugin;
namespace WBPluginTests.Fakes
{
public class FakeDoubleInputRetriever : DoubleInputRetriever
{
public double ReturnValue { get; set; }
public FakeDoubleInputRetriever()
{
}
public FakeDoubleInputRetriever(Double value)
{
ReturnValue = value;
}
public override double getUserInput(string prompt, double defaultValue)
{
return ReturnValue;
}
}
}
I am unable to run the unit tests because it can't find a certain AutoCAD assembly, which makes sense since I'm trying to do tests outside of AutoCAD so the assembly isn't available.
Is the problem that the parent class is trying to run but can't since the required assembly can't be found? Even though it's the child class that I'm using in the test, and the overridden method that would be used?
Inheritence is the strongest kind of dependency. Creating a fake implementing IUserInputRetriever<Double> decouples you from the concrete implementation that is depending on AutoCAD.

Two namespaces with the same object name

I have two namespaces:
System.Numerics and UnityEngine
Both have the type Vector3.
So now when ever i want to use it i have to declare which namespace before it. Like this:
protected struct CVN
{
public Complex h;
public UnityEngine.Vector2 d;
public UnityEngine.Vector3 n;
}
Is there any way to define that i only want Vector3 from one namespace so i don't have to always type NameSpaceHere.Vector3 every single time ?
Or am i stuck with the tedious nature of stating the namespace every time. Especially since i only need the Complex type from Numerics its quite annoying.
If all you need from System.Numerics is Complex, then:
using UnityEngine;
using Complex = System.Numerics.Complex;
At the top of your file, without using System.Numerics; should do it.
This is an alias.
You can wrap the using directive of the wanted class in the namespace of your current class rather than putting it outside.
Consider this example
namespace System.Numerics
{
class MyClass
{
}
}
namespace UnityEngine
{
class MyClass
{
}
}
using System.Numeric;
namespace ConsoleApplication24
{
using UnityEngine; // inside the namespace
class Program
{
static void Main(string[] args)
{
MyClass xx = new MyClass(); // from UnitEngine instead of System.Numeric
}
}
}

accessing a method in a dll

okay, so to start with i have set up the references in the project that i am useing the dll in.
what i am trying to do is access the method "haha" in my utils dll
code for dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Utils
{
public class kb
{
public class yes {
public void haha(string yes)
{
int test = Convert.ToInt32(yes);
}
}
}
}
and in the project im trying to access haha in i have just "Utils.kb.yes" but there is no method in that.. all i can do is Utils.kb.yes.equals and Utils.kb.yes.ReferenceEquals.
Since haha() is an instance method, you need to create an instance of the Utils.kb.yes class first:
Utils.kb.yes kb = new Utils.kb.yes();
kb.haha("nextproblem");
Or you also can make the method static:
public class yes {
public static void haha(string yes)
{
int test = Convert.ToInt32(yes);
}
}
then you can call it like this:
Utils.kb.yes.haha("I am static!");
Your classes do not have a constructor, and besides that, you simply CAN'T do much with a class before instantiating an object out of it. So you should reference your dll, and then create a new object first. From within that object, you can then reference your method(s).

How to derive a class from a nested interface?

The following code is not compilable:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args) { }
}
class Class : Class.Interface
{
internal interface Interface
{
}
}
}
The error message is:
error CS0146: Circular base class dependency involving 'ConsoleApplication1.Class' and 'ConsoleApplication1.Class.Interface'
Don't understand this.
Update:
This is probably more "motivating" (-;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args) { }
}
class Container : Container.Interface
{
// Everything, that is of type "Container.Interface" can be used as child here.
// ... including the container itself.
Interface[] _children;
// Is nested to keep the naming consistent.
internal interface Interface
{}
}
}
Wenn I put the interface outside of class "Container", it should be named somthing like "ContainerChildInterface". In my project I will have several classes like this, and thus several interfaces. And I think, using nested interfaces would be much better style in this case.

How to correct warning CS1707?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
using System.ComponentModel;
namespace ConsoleApplication1
{
class BreakingChange
{
delegate void SampleDelegate(string x);
public void CandidateAction(string x)
{
Console.WriteLine("Snippet.CandidateAction");
}
public class Derived : BreakingChange
{
public void CandidateAction(object o)
{
Console.WriteLine("Derived.CandidateAction");
}
}
static void Main()
{
Derived x = new Derived();
SampleDelegate factory = new SampleDelegate(x.CandidateAction);
factory("test");
}
}
}
\Program.cs(32,38): warning CS1707: Delegate 'ConsoleApplication1.BreakingChange.SampleDelegate' bound to 'ConsoleApplication1.BreakingChange.Derived.CandidateAction(object)' instead of 'ConsoleApplication1.BreakingChange.CandidateAction(string)' because of new language rules
\Program.cs(23,25): (Related location)
\Program.cs(16,21): (Related location)
Question:
I know what causes this warning and know the reason behind it. However, I don't know what the
best way to fix it?
1> Redefine the function (i.e.) change the function signature
2> Can we explicitly call BreakingChange.CandidateAction in the following line?
SampleDelegate factory = new SampleDelegate(x.CandidateAction);
Well, there are multiple ways to "fix" this, depending on what you want to, and can, do.
Personally I would add another overload to Derived that took a string, since you're going to have the same issue with non-delegate calls as well.
public class Derived : BreakingChange
{
public new void CandidateAction(string x)
{
base.CandidateAction(x);
}
public void CandidateAction(object o)
{
Console.WriteLine("Derived.CandidateAction");
}
}
Or, since you know you want the base class method, you can cast the reference x:
new SampleDelegate(((BreakingChange)x).CandidateAction)

Categories

Resources