Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions Ical.Net/CalendarObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license.
//

#nullable enable
using System;
using System.Runtime.Serialization;
using Ical.Net.Collections;
Expand All @@ -14,8 +15,9 @@ namespace Ical.Net;
/// </summary>
public class CalendarObject : CalendarObjectBase, ICalendarObject
{
private ICalendarObjectList<ICalendarObject> _children;
private ServiceProvider _serviceProvider;
// Are initialized in the constructor
private ICalendarObjectList<ICalendarObject> _children = null!;
private ServiceProvider _serviceProvider = null!;

internal CalendarObject()
{
Expand Down Expand Up @@ -50,11 +52,11 @@ private void Initialize()

protected virtual void OnDeserialized(StreamingContext context) { }

private void Children_ItemAdded(object sender, ObjectEventArgs<ICalendarObject, int> e) => e.First.Parent = this;
private void Children_ItemAdded(object? sender, ObjectEventArgs<ICalendarObject, int> e) => e.First.Parent = this;

protected bool Equals(CalendarObject other) => string.Equals(Name, other.Name, StringComparison.OrdinalIgnoreCase);

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
Expand Down Expand Up @@ -89,7 +91,7 @@ public override void CopyFrom(ICopyable c)
/// <summary>
/// Returns the parent iCalObject that owns this one.
/// </summary>
public virtual ICalendarObject Parent { get; set; }
public virtual ICalendarObject? Parent { get; set; }

/// <summary>
/// A collection of iCalObjects that are children of the current object.
Expand All @@ -99,18 +101,18 @@ public override void CopyFrom(ICopyable c)
/// <summary>
/// Gets or sets the name of the iCalObject. For iCalendar components, this is the RFC 5545 name of the component.
/// </summary>
public virtual string Name { get; set; }
public virtual string? Name { get; set; }

/// <summary>
/// Gets the <see cref="Net.Calendar"/> object.
/// The setter must be implemented in a derived class.
/// </summary>
public virtual Calendar Calendar
public virtual Calendar? Calendar
{
get
{
ICalendarObject obj = this;
while (obj is not Net.Calendar && obj.Parent != null)
while (obj is not Net.Calendar && obj?.Parent != null)
{
obj = obj.Parent;
}
Expand Down Expand Up @@ -140,9 +142,9 @@ public virtual Calendar Calendar

public virtual void RemoveService(string name) => _serviceProvider.RemoveService(name);

public virtual string Group
public virtual string? Group
{
get => Name;
set => Name = value;
}
}
}
16 changes: 7 additions & 9 deletions Ical.Net/CalendarObjectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// Licensed under the MIT license.
//

#nullable enable
using System;

namespace Ical.Net;

// This class should be declared as abstract
public class CalendarObjectBase : ICopyable, ILoadable
public abstract class CalendarObjectBase : ICopyable, ILoadable
{
private bool _mIsLoaded = true;

Expand All @@ -17,32 +17,30 @@ public class CalendarObjectBase : ICopyable, ILoadable
/// to the current object. This method must be overridden in a derived class.
/// </summary>
public virtual void CopyFrom(ICopyable obj)
{
throw new NotImplementedException("Must be implemented in a derived class.");
}
=> throw new NotImplementedException("Must be implemented in a derived class.");

/// <summary>
/// Creates a deep copy of the <see cref="T"/> object.
/// </summary>
/// <returns>The copy of the <see cref="T"/> object.</returns>
public virtual T Copy<T>()
public virtual T? Copy<T>()
{
var type = GetType();
var obj = Activator.CreateInstance(type) as ICopyable;

if (obj is not T objOfT) return default(T);
if (obj is not T objOfT) return default(T?);

obj.CopyFrom(this);
return objOfT;
}

public virtual bool IsLoaded => _mIsLoaded;

public event EventHandler Loaded;
public event EventHandler? Loaded;

public virtual void OnLoaded()
{
_mIsLoaded = true;
Loaded?.Invoke(this, EventArgs.Empty);
}
}
}
Loading